Get DirectoryName for folder

Marcus Östman 21 Reputation points
2020-09-08T12:04:16.967+00:00

I have this script which exports a list of files and folders to a csv.

Get-ChildItem -path D:\VmWare -Recurse | Select-Object DirectoryName, Name, Extension, Length | Export-Csv -Path D:\Temp\Test.csv -Encoding Unicode -NoTypeInformation

I trying to figure out if it's possible to get DirectoryName for a folder. If I choose 'Fullname' I also get file name on files, which I don't want to.
I found this line, but I don't see how to incorporate it to the script above.

foreach {Split-Path -Path $_.Fullname -Parent | Split-Path -NoQualifier }

/api/attachments/23228-screenshot-1.jpg?platform=QnA

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,546 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 46,801 Reputation points
    2020-09-08T14:44:27.447+00:00

    Do you want only directories in the list? If so, add the "-Directory" switch to the Get-ChildItem cmdlet.

    If you want directories and files in the list, then this should help:

    Get-ChildItem c:\junk -Recurse | 
        ForEach-Object {
            if ($_.PSIsContainer){
                $_.FullName
            }
            else{
                $_.DirectoryName
            }
        }
    
    1 person found this answer helpful.
    0 comments No comments

  2. Keith Miller 6 Reputation points
    2020-09-09T11:35:06.287+00:00

    Make DirectoryName a calculated property:

    Get-ChildItem -path D:\VmWare -Recurse |
        Select @{ N = 'DirectoryName' ; E = {
            If ( ! ( $_.PSIsContainer ))
                { $_.DirectoryName }
            Else
                { Split-Path $_.FullName }
        }}, Name, Extension, Length |
    Export-Csv -Path D:\Temp\Test.csv -Encoding Unicode -NoTypeInformation
    
    1 person found this answer helpful.
    0 comments No comments

  3. Ian Xue 37,541 Reputation points Microsoft Vendor
    2020-09-09T03:03:53.117+00:00

    Hi,

    According to your screenshot, are you trying to get the full path of the parent directory of a directory? If yes you may try this

    Get-ChildItem -path D:\VmWare -Recurse -Directory | ForEach-Object { Split-Path -Path $_.FullName -Parent }  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.