Welcome › Forums › General PowerShell Q&A › Replacing a sub-string with wildcard
I have a string like “F:\Backup” I need to replace “F:\” with nothing. But instead of “F” it could be some other drive, like “D:\Backup”, so I need a wildcard. Something like:
$BackupFolder = 'F:\Backup'
$NewFolder = $BackupFolder -replace "?:\" , ""
Since -replace used regex, you will need to pattern match and backslash scape special regex characters (backslash is special here) that need to be handled literally. See regex for explanation
Or “.” in regex means any character. You can leave off the 2nd arg if it’s null. https://regex101.com is a good site to try out patterns.