I have the following tree structure of a folder called test.
What I am trying to achieve is to list the main directory and its sub-directories under the column directory name using the Get-ChildItem property(Code is shown below).
Here's the code that I wrote in powershell :
Get-ChildItem -Path C:\Users\USER\Desktop\test -Recurse | Select-Object directoryname, Name, LastWriteTime, CreationTime | Export-Csv -Path 'C:\Users\USER\Desktop\filetxt.csv' -Encoding ascii -NoTypeInformation
What I achieved is the following :
As you can see, the directory name for Folder1 and Folder2 is missing and it should've been taken the same name of the path. Similarly for SubFolder1, the directory name should be Main Path\Folder1 and etc... My expected result is the following :
Can someone please help me find a solution for the following problem ? Thank you
Folders do not have a property "directoryname". You can use a calculated property with a Split-Path
to extract the information you're after.
Get-ChildItem -Path D:\sample\test-files -Recurse |
Select-Object -Property @{Name='Path';Expression={Split-Path -Path $_.FullName -Parent}},Name, LastWriteTime, CreationTime, @{Name='Extension';Expression={if (-not $_.PSIsContainer) {$_.Extension}}}
And BTW: Folders actually do not have a "LastWriteTime". They show the "LastWriteTime" from the newest file they contain. ;-)