0

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)"}
    
    }
  • Have a look at [Format-Table](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/format-table?view=powershell-5.1) – Vivere May 13 '21 at 07:17

1 Answers1

0

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"
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • How to change this script to not print the object names (fulln and shrtn) ? – Dhanushka Ekanayake May 13 '21 at 12:26
  • @DhanushkaEkanayake Simply type names you do want there.. – Theo May 13 '21 at 14:03
  • @DhanushkaEkanayake Use the following: `| Format-Table -HideTableHeaders` at the very end of the pipeline - or use `PathToYourScript.ps1 | Format-Table -HideTableHeaders` with the script unchanged. – JosefZ May 13 '21 at 14:22
  • Can you please tell me where do I should add the "| Format-Table -HideTableHeaders" in the script? (I add it in the very end, But it does not work) – Dhanushka Ekanayake May 14 '21 at 03:15