none
Get-ChildItem RRS feed

  • Question

  • I am trying to list out all child folders from a  CSV list of server shares. The output will be read by another script to get the child folder sizes however, the output file is also populated with the server share root folder. How do I prevent the root share from being added to the output file?  Script:

    $InputFile = "C:\Temp\Intput.csv" $OutputFile = "C:\Temp\Output.csv" Import-Csv $InputFile | ForEach-Object{ Write-Host "Listing child folders from: " -NoNewline; Write-host $($_.FullName) (Get-ChildItem $_.FullName | Where-Object { ($_.PSIsContainer) } | Select-Object -Property Parent, Fullname) [PSCustomObject]@{ Parent = $_.Parent; 'FullName' = $_.FullName; } }| Select-Object Parent, 'FullName' | Sort-Object Parent, 'FullName' | Export-csv -NoTypeInformation $OutputFile # The input file format is: # # "Parent","FullName" # "Share1$","\\Server\Share1$"
    # "Share2$","\\Server\Share2$"

    # The output file shows as follows # "Parent","FullName" # "Share1$","\\Server\Share1$" # "Share1$","\\Server\Share1$\Backups" # "Share1$","\\Server\Share1$\Restricted" # "Share2$","\\Server\Share2$" # "Share2$","\\Server\Share2$\Data"
    # "Share2$","\\Server\Share2$\Test"



    Wednesday, January 9, 2019 3:37 PM

Answers

  • $InputFile  = 'C:\Temp\Intput.csv'
    $OutputFile = 'C:\Temp\Output.csv'
    
    Import-Csv $InputFile |
        ForEach-Object{
            Write-Host "Listing child folders from: $($_.FullName)" -Fore Green
            Get-ChildItem $_.Fullname -Directory
        } |
        Select-Object Parent, Fullname |
        Sort-Object   Parent, FullName |
        Export-csv -NoTypeInformation $OutputFile


    \_(ツ)_/


    • Edited by jrv Wednesday, January 9, 2019 4:05 PM
    • Marked as answer by TheAnalogKid Wednesday, January 9, 2019 4:13 PM
    Wednesday, January 9, 2019 4:05 PM

All replies

  • $InputFile  = 'C:\Temp\Intput.csv'
    $OutputFile = 'C:\Temp\Output.csv'
    
    Import-Csv $InputFile |
        ForEach-Object{
            Write-Host "Listing child folders from: $($_.FullName)" -Fore Green
            Get-ChildItem $_.Fullname -Directory
        } |
        Select-Object Parent, Fullname |
        Sort-Object   Parent, FullName |
        Export-csv -NoTypeInformation $OutputFile


    \_(ツ)_/


    • Edited by jrv Wednesday, January 9, 2019 4:05 PM
    • Marked as answer by TheAnalogKid Wednesday, January 9, 2019 4:13 PM
    Wednesday, January 9, 2019 4:05 PM
  • Brilliant! Thank you very much.
    Wednesday, January 9, 2019 4:12 PM