PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,592 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have in multiple locations text files named pc01; pc02; pc03 and so on up to pc18.
I want to go through them in order and get the contant into a single file.
here's the script I use for example:
$source = "C:\Users\Admin\Downloads\80\pc"
$dest = "C:\Users\Admin\Downloads\80\all.txt"
$0 = 0
$1 = 1..9 | ForEach-Object { (Get-Content -Path $source$0$_.txt ) }
$10 = 10..18 | ForEach-Object { (Get-Content -Path $source$_.txt ) }
$1, $10 | Out-File -FilePath $dest
I'm looking to learn how to get it shorter in 2 ways:
and if you have another suggestion how - I'd like to learn!
thanks!
Here's one way.
$source = "C:\Users\Admin\Downloads\80\pc"
$dest = "C:\Users\Admin\Downloads\80\all.txt"
$data = 1..18 | ForEach-Object {
$filename = "{0}{1:D2}.txt" -f $source, $_
write-host $filename # display the filename that we built to console
Get-Content $filename # read content and put in to $data variable
}
"We collected {0} characters." -f $data.Length
$data | Out-File -FilePath $dest