Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Powershell – Matches [1] don't work with "|"

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

$NameOrigin = "BP \|"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading