I wrote a PowerShell script for a superuser question:
list of all files and directories under a path recursively, without recursing into junction points or links
I have a strong feeling that my code is way too complicated for such a simple task.
function Recurse($path) {
$fc = new-object -com scripting.filesystemobject
$folder = $fc.getfolder($path)
foreach ($i in $folder.files) { $i | select Path }
foreach ($i in $folder.subfolders) {
$i | select Path
if ( (get-item $i.path).Attributes.ToString().Contains("ReparsePoint") -eq $false) {
Recurse($i.path)
}
}
}
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$outputlist = Recurse($scriptPath) | Out-File -Filepath .\outputlist.txt
Short instructions on how to use the script:
- Paste the code into a text file and save it as PowerShell (.ps1) script
- Place the script inside a folder to list all files+folder recursively without junction folder loops
- Execute the script. A new file called
outputlist.txt
at the same location will appear
Questions:
- Can I simplify this PowerShell script anyhow?
- Is there a better Out-command where no blank spaces are appended after every line?