I'm trying to make using of String.Substring() to replace every string with its substring from a certain position. I'm having a hard time figuring out the right syntax for this.
$dirs = Get-ChildItem -Recurse $path | Format-Table -AutoSize -HideTableHeaders -Property @{n='Mode';e={$_.Mode};width=50}, @{n='LastWriteTime';e={$_.LastWriteTime};width=50}, @{n='Length';e={$_.Length};width=50}, @{n='Name';e={$_.FullName -replace "(.:.*)", "*($(str($($_.FullName)).Substring(4)))*"}} | Out-String -Width 40960
I'm referring to the following expression
e={$_.FullName -replace "(.:.*)", "*($(str($($_.FullName)).Substring(4)))*"}}
The substring from the 4th character isn't replacing the Full Name of the path. The paths in question are longer than 4 characters.
The output is just empty for the Full Name when I run the script. Can someone please help me out with the syntax
EDIT
The unaltered list of strings (as Get-ChildItem recurses) would be
D:\this\is\where\it\starts D:\this\is\where\it\starts\dir1\file1 D:\this\is\where\it\starts\dir1\file2 D:\this\is\where\it\starts\dir1\file3 D:\this\is\where\it\starts\dir1\dir2\file1
The $_.FullName will therefore take on the value of each of the strings listed above.
Given an input like D:\this\is or D:\this\is\where, then I'm computing the length of this input (including the delimiter \) and then replacing $_.FullName with a substring beginning from the nth position where n is the length of the input.
If input is D:\this\is, then length is 10. Expected output is
\where\it\starts \where\it\starts\dir1\file1 \where\it\starts\dir1\file2 \where\it\starts\dir1\file3 \it\starts\dir1\dir2\file1
D:\you should do exactly that:$_.FullName -replace '^.:\\'. If you want to remove a particular parent path you could do this:$_.FullName -replace ('^' + [regex]::Escape($parent)). If you must use a fixed length you could do this:$_.FullName -replace '^.{4}'.