Replacing a sub-string with wildcard

Hi,

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 "?:" , “”

Any ideas?

Thanks.

 

 

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

$folder -replace '^[a-z]:\\'

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.

'f:\backup' -replace '.:\\'

backup