'Save directory tree to CSV, along with whether element is a file or folder in powershell
So far I've got this:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, name |
Export-Csv -path 'C:\output.csv' -noTypeInfo
It gives me a CSV with the full path name, and name of each folder & file in a directory.
However, I'd like a 3rd column that has either 'Folder' or 'File' (or something similar). Basically just a column that explicitly tells me whether something is a file or folder.
I feel like it should be a simple case of adding a new column like FullName, name, Type - but not sure what options are available.
Is this possible?
Solution 1:[1]
You could use the Attributes property however, this might give you more information than you really need, see FileAttributes Enum.
If you simply need a property with 2 values (File or Directory) you can use the boolean property PSIsContainer as reference to construct your new calculated property:
Get-ChildItem -Recurse 'C:\MyFolder' |
Select-Object FullName, Name, @{N='Type';E={('File', 'Directory')[$_.PSIsContainer]}} |
Export-Csv -path 'C:\output.csv' -NoTypeInformation
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
