Reputation: 439
I am trying to assign all child paths of files in a directory to an array but not assigning the directories.
I am using the following command:
$a = Get-ChildItem -path "C:\test\" -recurse | ?{!$_.PSIsContainer } | % { Write-Host $_.FullName }
The output is printed to the console and not saved in the variable. Any idea way?
Upvotes: 0
Views: 187
Reputation: 7625
The console output from Write-Host
is only displayed, not returned.
You will need to save the value in a variable first, then you can print it:
$a = Get-ChildItem -path "C:\test\" -recurse | ?{!$_.PSIsContainer };
$a | % { Write-Host $_.FullName }
Upvotes: 0
Reputation: 500
The reason it is writing to the console is because you are saying Write-Host. If you use Write-Output instead, it will save to the variable.
$a = Get-ChildItem -path "C:\test\" -recurse | ?{!$_.PSIsContainer } | % { Write-Output $_.FullName }
Or try using the "Select-Object" instead of write host.
It would look like this:
$a = Get-ChildItem -path "C:\test\" -recurse | ?{!$_.PSIsContainer } | select-object -expand FullName
Upvotes: 2