Remove Options from Command String

by Jan 31, 2012

Let's assume you'd like to remove all options from a raw text command such as this one:

xcopy "C:\Some Folder" "C:\Some New Folder Name" /y /r /Q

Since all options start with "/" and are a single character, you can derive a pattern and use a regular expression replace operation. Have a look:

PS> 'xcopy "C:\Some Folder" "C:\Some New Folder Name" /y /r /Q' -replace '/.', $null
xcopy "C:\Some Folder" "C:\Some New Folder Name"

All options are removed. -replace was looking for a "/" and then any other character (".") and replacing every occurrence with nothing ($null).

Twitter This Tip! ReTweet this Tip!