Extract string from within string

Hello, I am trying to get 1 piece of information from a string. I run a command it gives me the below information. SubString() does not work on this for some reason. I have dumped the below to a .TXT file and used Get-Content and SubString() fails.

**ClassInformation **
**---------- **
CIM_Chassis (CreationClassName = “CIM_DockingStation”, Tag = “5T0KD4R”)

In PowerShell how do I get just the Tag value? in this case 5T0KD4R

Thank you.

Hi :wave:t4: and welcome to the forum. Please when posting code or errors, please format it as code with the preformatted button <\>. Simply click that button and paste the code. You can see your double quotes have been converted to some fancy quotes that will cause errors if copy/pasted into powershell.

I can tell you how to extract the value, but first I’d be interested in seeing how you retrieve the value initially. There is likely a better/easier way to extract the value without resorting to string manipulation.


Hello, thank you for the quick reply. Unfortunately wrapping everything in () does not work. The TAG can’t be called by itself and the only way to get that number is to tweak the string.

Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_Docked | Select-Object Antecedent

That is the actual command.

Any help would be awesome.

A little bit more information would have been nice …

Without the chance to reproduce it I’ll take a wild guess and suggest the following snippet:

Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_Docked | 
    Select-Object -ExpandProperty Antecedent | 
        Select-Object -ExpandProperty CIM_Chassis | 
            Select-Object -Property Tag

… or in another way …

(Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_Docked).Antecedent.CIM_Chassis.Tag
1 Like

Hello, thank you sir. This command worked:

(Get-CimInstance -Namespace root/DCIM/SYSMAN -ClassName DCIM_Docked).Antecedent.Tag

It returned just the asset tag of the WD19TB dock I am using.


And way more precise than string manipulation. However, to answer that question, assuming it’s a single string, here are several methods to manipulate the tag out of the string.

$str = 'CIM_Chassis (CreationClassName = "CIM_DockingStation", Tag = "5T0KD4R")'

$str -replace '.+Tag = "|"\)'

$str -replace '.+Tag = "(.+)"\)','$1'

if($str -match 'Tag = "(.+)"\)'){

[regex]::Match($str,'(?<=Tag = ")(.+)(?="\))').value

switch -Regex ($str){
    '.+Tag = "(.+)"\)' {$Matches.1}

($str -split '"')[3]

Also, please take the time when posting in the forum, to format code as code. There’s a little button up top that looks like </> - simply click it and paste the code in the highlighted area.

1 Like