1

I am trying to create a script that select the four numbers that the company computer have in the host name.

I have tested the regex '\d{4}' in a regex web site, and it works fine to select the four numbers. but when using it with powershell y only get the $true or $false.

I need that the 4 numbers are keept in a variable for later use but i havent achieved it.

any ideas??

$machinename = "mac0016w701"
$test = $machinename -match '\d{4}'

$test2= Select-String -Pattern '\d{4}' -inputobject $machinename
$test2

1 Answer 1

1

-match is an operator which returns true/false, so you can use it in tests. If you want the values from the regex, it sets the magic variable $Matches, e.g.

PS D:\> 'computer1234' -match '\d{4}'
True
PS D:\> $matches[0]
1234

Alternately, you could use:

[regex]::Matches('computer1234', '\d{4}').Value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.