Powershell Parsing Help - How to output a list of folder names into a text file
Solution 1
Try this one:
Get-ChildItem C:\Source\Path | ForEach-Object { $_.Name } > C:\Output\File.txt
Related resources:
Solution 2
You can get just the names with the -Name switch:
$accFolder = Read-Host "Enter the account folder container....: "
Get-ChildItem -Name "D:\influxcyst\$accFolder" | Out-File $HOME\desktop\$accFolder.txt
Matthew
Updated on July 09, 2022Comments
-
Matthew almost 2 years
I have a simple script that reads the folder names and outputs them into a text file. I realized I got a lot more output then I wanted, so I used the select-item cmdlet to select just the name property from the hashtable. The problem is that there is still all the white space that the data I omitted would have normally filled, not helping my problem since the white space will destroy my script.
I have tried some [regex] commands to strip out the whitespace with (/S+) but I don't really know it that well I was using some code trying to tweak from an example someone helped me with. The topic name is the same as the title here and it is on this site too. Anyone that can help me I would appreciate it!
Basically, I cant figure out how to output the names of folders into a simple text file with ZERO whitespace (1 line per folder name).
$accFolder = Read-Host "Enter the account folder container....: " $dataArray = Get-ChildItem "D:\influxcyst\$accFolder" | select-object name $dataArray $dataArray | Out-File $HOME\desktop\$accFolder.txt $newArray = get-content $HOME\desktop\$accFolder.txt #[regex]$regex = "\s(\S+)\s" #[regex]::matches($newArray,$regex) | foreach-object {$_.groups[1].value}
-
Matthew almost 13 yearsdoh i didn't even think about using foreach like that. Thank you that did the trick!!
-
Matthew almost 13 yearsQuick question, is the > synonymous with | outfile ?
-
Michał Poreda almost 13 years@Matthew Yes, the
>
operator redirects the output to a file. -
Michał Poreda almost 13 years@Matthew Would you please mark this as answer if it was helpful?