Remove Options from Command String

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!


Posted Jan 31 2012, 06:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.