Home > Blockchain >  Powershell - Matches [1] don't work with "|"
Powershell - Matches [1] don't work with "|"

Time:11-29

1

This is part of the code I created:

$NameOrigin = "BP |"
$NameBackup = "Backup"
Get-CimInstance win32_logicaldisk |
 ForEach-Object{
     if ($_.VolumeName -match "^$NameOrigin(. )" -and $_.VolumeName -notmatch "^$NameBackup(. )" ){
         $Employee = $Matches[1]
         $FSRootOrigin = "{0}{1}" -f $_.DeviceId,'\'
     }
 }

Basically it checks if there is any drive with the word "BP |" and that does not contain the word "Backup" in the volume label and gets the letter of that drive.

Given that, if you look at the $Employee variable, the idea is that it extracts what is after "|" so I can use it in another step of the script, however for some reason it doesn't work when the drive is named using "|".

When the $NameOrigin variable was "BP -" I was able to extract the information that was after the "-".

Why doesn't it work using "|"?

How can I get around this?

CodePudding user response:

I guess the answer is to escape the pipe because it's regex for "or".

$NameOrigin = "BP \|"
  • Related