Removing Options from Command String (Enhancement)

by Feb 1, 2012

In a previous tip we showed how you can remove options from a command line text using -replace. Here is a more versatile approach. It will remove any character or word that starts with "/" and then remove all trailing spaces as well:

PS> $command = 'somecommand "C:\Some Folder" "C:\Some New Folder Name" /y /r /quiet /simple'
PS> ($command -replace '/\w*', $null).trim()
somecommand "C:\Some Folder" "C:\Some New Folder Name"

Twitter This Tip! ReTweet this Tip!