0

In the powershell command-line I can execute the following

$list = Get-ChildItem $path -name
foreach($k in $list){Write-host $k}

And it will list all the filenames in $path

But if I copy and paste the same thing in code to execute, the output is blank

Why would this even happen?

2 Answers 2

3

Write-Host will write the string directly to the Host aka. the command line window, nowhere else

Just replace Write-Host with Write-Output if you want $k written to STDOUT:

$list = Get-ChildItem $path -name
foreach($k in $list){Write-Output $k}

But in reality, that is unnecessary, Write-Output is called implicitly. You could achieve the same thing with just:

Get-ChildItem $path -Name
Sign up to request clarification or add additional context in comments.

Comments

0

I wanted just a list of files sorted by size without directory headers. I used this to accomplish it.

get-childitem foldername -file -recurse | select FullName,Length | sort Length -descending | export-csv c:\export.csv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.