Replacing a sub-string with wildcard

Welcome Forums General PowerShell Q&A Replacing a sub-string with wildcard

Viewing 2 reply threads
  • Author
    Posts
    • #255410
      Participant
      Topics: 10
      Replies: 17
      Points: 42
      Rank: Member

      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.

       

       

    • #255416
      Participant
      Topics: 0
      Replies: 81
      Points: 362
      Helping Hand
      Rank: Contributor

      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

    • #255419
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,690
      Helping Hand
      Rank: Community Hero

      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.

      • This reply was modified 2 weeks, 2 days ago by js.
      • This reply was modified 2 weeks, 2 days ago by js.
Viewing 2 reply threads
  • You must be logged in to reply to this topic.