Question:
I’m trying to do the following:
1 2 3 4 5 |
$files = Get-ChildItem c:\temp | Select-Object Name foreach ($i in $files) { Write-Host "Filename is $i" } |
Sample result:
1 2 3 |
Filename is @{Name=oracle10204.rsp} Filename is @{Name=powershell.txt} |
How do I get only the following?
1 2 3 |
Filename is oracle10204.rsp Filename is powershell.txt |
Answer:
Here is the answer to get just the name from your example. Surround the $i with $( ) and reference the .Name property. The $() will cause it to evaluate the expression.
1 2 3 4 5 |
$files = Get-ChildItem c:\temp | Select-Object Name foreach ($i in $files) { Write-Host "Filename is $($i.Name)" } |