问题
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