20Aug/07
More PowerShell One-Liners
A one-liner used today while fiddling around with a robocopy output from moving some PST files around. I wanted to take the messy robocopy log file and just extract the PST file names and paths from the log file (yes, I know, they could have used some of the robocopy switches like /NS /NJH /NJS, but they wanted the other info for other purposes --- all I wanted was the filenames).
A little PowerShell, combined with some .NET regex fun did the trick. The breakdown...
- Get the text file contents
- Grab only the lines with the format of \\
.pst - Split those lines (to get rid of the whitespace and size output) at the "\\"
- Add the path (which is all that is left over now in $1[1]) to a list variable $files, adding the \\ along the way.
get-content .\pstrobocopy.txt | where { $_ -match "\\\\.+?\.pst$" } ; foreach { $line = [regex]::split($_,'\\\\'); $files += "\\" + $line[1] }