Search code examples
powershelldirectorysubdirectory

How to get this PowerShell output as a Table view?


I need to get this PowerShell output in a Table view.Also, need to within quotation marks.

Current Output format:

Testing\Dump\DumpText-1.txt      Dump\DumpText-1.txt
Testing\Dump\DumpText-2.txt      Dump\DumpText-2.txt
Testing\Dump\SubDump1\DumpText-1.txt     SubDump1\DumpText-1.txt
Testing\Dump\SubDump1\DumpText-2.txt     SubDump1\DumpText-2.txt
Testing\Dump\SubDump2\Screenshot.png     SubDump2\Screenshot.png

Required Output Format:

"Testing\Dump\DumpText-1.txt"            "Dump\DumpText-1.txt"
"Testing\Dump\DumpText-2.txt"            "Dump\DumpText-2.txt"
"Testing\Dump\SubDump1\DumpText-1.txt"   "SubDump1\DumpText-1.txt"
"Testing\Dump\SubDump1\DumpText-2.txt"   "SubDump1\DumpText-2.txt"
"Testing\Dump\SubDump2\Screenshot.png"   "SubDump2\Screenshot.png"

My Script is:

$directoryPath=$args[0]

Get-ChildItem $directoryPath -Recurse -Force | ForEach-Object -Process  { 
        
        if (!$_.PSIsContainer) {"$($_.FullName -creplace '^[^\\]*\\', '') `t` $($_.Directory.Name)\$($_.Name)"}
    
    }


Solution

  • Try PSCustomObject (and follow Quoting Rules):

    Get-ChildItem $directoryPath -Recurse -Force -File |
        ForEach-Object -Process  { 
            [PSCustomObject]@{    
                fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
                shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
            }
        }
    

    Edit

    To hide the column headings from the table, apply Format-Table cmdlet as follows (read more at Controlling column widths with Format-Table):

    Get-ChildItem $directoryPath -Recurse -Force -File |
        ForEach-Object -Process  { 
            [PSCustomObject]@{    
                fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
                shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
            }
        } | Format-Table -HideTableHeaders -AutoSize
    

    However, Format- cmdlets are designed for console/screen output only. Read more in Problem with Format- cmdlets

    Advanced script:

    Param(
        [Parameter(Position=0, Mandatory=$false, ValueFromPipeline)]
        [string]$directoryPath='\bat\filez',
        [Parameter()]
        [switch]$AsObject
    )
    
    $outObj = Get-ChildItem $directoryPath -Recurse -Force -File |
        ForEach-Object -Process  { 
            [PSCustomObject]@{    
                fulln = """$($_.FullName -creplace '^[^\\]*\\', '')"""
                shrtn = """$(Join-Path -Path $_.Directory.Name -ChildPath $_.Name)"""
            }
        }
    
    if ( $AsObject.IsPresent ) {
        $outObj | Out-Default
    } else {
        $outObj | Format-Table -HideTableHeaders -AutoSize
    }
    

    Example 1: .\SO\67514630.ps1

    "bat\filez\more_real.eml"      "filez\more_real.eml"
    "bat\filez\PS_preferences.bat" "filez\PS_preferences.bat"
    "bat\filez\Sample Input.eml"   "filez\Sample Input.eml"
    "bat\filez\SampleInput.eml"    "filez\SampleInput.eml"
    "bat\filez\folder\xxx.csv"     "folder\xxx.csv"
    

    Example 2: .\SO\67514630.ps1 \bat\foo.txt -AsObject

    fulln                       shrtn
    -----                       -----
    "bat\files\676711\foo.txt"  "676711\foo.txt"
    "bat\files\bubu\foo.txt"    "bubu\foo.txt"
    "bat\Unusual Names\foo.txt" "Unusual Names\foo.txt"
    "bat\foo.txt"               "bat\foo.txt"