Use child-item result object in array

一笑奈何 提交于 2020-01-11 11:30:36

问题


I encountered a problem in PowerShell in listing its child items.

$file = Get-ChildItem \\compname\c$\folder\ -Recurse -Filter *filename.txt* |
        Select-Object -Property DirectoryName, FullName

When I try this to get its objects it was empty:

$file.FullName

or

$file.DirectoryName

If there is a many files in that directory with the same file name, how can I backup up those files in the same folder by by adding .bak on its file extension.


回答1:


You're still using PowerShell v2 or earlier. These early versions don't support member enumeration on arrays, which would allow you to access properties of array elements via the array object itself. Instead you get an empty result, because the array object does not have a property DirectoryName or FullName.

If you can't upgrade to at least PowerShell v3 you can work around this issue with a loop:

$file | ForEach-Object { $_.FullName }

or by expanding the property:

$file | Select-Object -Expand FullName


来源:https://stackoverflow.com/questions/39590022/use-child-item-result-object-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!