Powershell String

I’m new to powershell, so bear with me. I’m trying to do something I thought would be easy and common: find file paths that are “too long” (greater than some value. i.e. 260 character). The below code runs fine, but while I can either output the file paths or and I can pipe to Select-Object and get the string length, I can’t figure out how to have them both. Get-Member shows $longpathfiles as a ‘System.String’ type, with the only property being ‘Length’. What I want to end up with is a 2 column table. ‘PathLength’, and ‘Path’ I can pipe to Out-Gridview or csv. So…what am I missing? Thanks in advance.

$longpathfiles =  robocopy . NULL /L /s /njh /njs /fp /ns /nc /ndl | where length -gt 260 | foreach { $_.trim()  }


Almost there… You just need to create a PSCustomObject foreach paths with the path name and length

$longpathfiles = robocopy . NULL /L /s /njh /njs /fp /ns /nc /ndl | Where-Object length -gt 260 | ForEach-Object -Process {
    if ($Null -ne $_.trim()) {
            Path = $_.trim()
            Length = $_.Trim().Length

Thanks for the quick reply. This works great.


But question though, why test against $NULL if I’m already filtering out anything with less than 260 character?

You could have lines with 261 white spaces. if ($_.Trim()) should suffice though.

Foreach-Object processes a lines in the robocopy output, so if there is any empty lines coming in, this will act as a safety check. Its always good to have null checks in the code if we suspect such situation.