0

I am trying to declare and set a variable in a powershell command. Is that possible?

I was hoping to do something like this:

"$name" = "I219" | Get-NetAdapter | where Name -Match "$name"

Is this possible or can this only be done in a .ps script?

1
  • Starting with PSv3 the variant without where scriptblock is possible, just the first | should be a ; and the double quotes enclosing variable name for output are possible but not neccessary on assigning a variable they are illegal. Commented Nov 15, 2018 at 15:17

1 Answer 1

1

It can be done easily by just hitting enter in the console after declaring your variable:

$name = "I219" # now hit enter

To access the variable, type it in the console and hit enter again:

$name # hit enter => returns I219

Now use it with your command:

Get-NetAdapter | where { $_.Name -Match $name }

Or as a one-liner:

$name = "I219"; Get-NetAdapter | where { $_.Name -Match $name }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.