Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Saturday, May 5, 2012

Powershell Regular Expressions Save the Day

Powershell Regular Expressions Save the Day

Copy a string from between two other characters/strings (as in a UNC Path).

I had a UNC path like this:
\\server\share\folder\folder\folder\file.txt

I needed to rip out just the server part of that whole mess, which I did so with this regular expression (after fooling with it for way too long!!):

$TargetUNC = "\\server\share\folder\folder\folder\file.txt"
$ServerName = [regex]::match($TargetUNC,'[^\\]+').value
#The line above will strip out "server" from the UNC string and save it to variable $ServerName

No comments:

Post a Comment