PowerShell Variables


This entry is part 4 of 9 in the series PowerShell

You can create variables while using PowerShell. You always must preface the name with a dollar sign. If you want to create a variable called Amount you would write it this way: $Amount. Variable names aren’t case-sensitive and can include spaces and special characters. However, variable names that include special characters and spaces are difficult to use and should not be used.

You can store data in your variable. That data could be the result of a cmdlet that was shown in a previous post. So it’s not just a simple number that a variable may contain. Variables may contain objects. For example, we could run $Browser = Get-Process Chrome.

To illustrate the basics, here is a very short session in PowerShell. All we are doing here is creating a variable called $Thing that we store a zero in. We can use this variable $Thing in our commands.

Here below we illustrate just evaluating the string and displaying it. Variables must start with the $ character. Here we’ve assigned zero to $Thing. We could have assigned a string like this: $Message = “Hello World”.

PS C:\WINDOWS\system32> $Thing = 0
PS C:\WINDOWS\system32> $Thing
0
PS C:\WINDOWS\system32>                                                                   

Objects

Below is another script. Look at the last line. You can type $Calc. and hit the Tab key twice and you might see what’s shown below. The Name of the $Cacl object. What is the CompanyName? Microsoft Corporation. Continue to press the Tab key to scroll through. You can also Kill processes with Object.Kill(). Since Kill() is a method you will need to use the two brackets after Kill.

PS C:\WINDOWS\system32> $Calc = get-process calculator
PS C:\WINDOWS\system32> $Calc
Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    568      28    39392        296       1.03   5384   1 Calculator


PS C:\WINDOWS\system32> $Calc.Name  
PS C:\WINDOWS\system32> $Calc.Company
Microsoft Corporation
PS C:\WINDOWS\system32>  

Below is just a list of commands you could try. They start two instances of Notepad and then closes them with the kill() method

notepad
notepad
get-process notepad
get-process -name notepad
(get-process -name notepad).kill
(get-process -name notepad).kill()

Below are some more examples.

notepad
get-process notepad 
notepad
get-process notepad
get-process -name notepad | sort-object -property id
get-process -name notepad | sort-object -property id -descending
get-process -name notepad | sort-object -property id -descending | stop-process

Variables

These commands can be stores in a variable that is really an object, not simply a string. Since it is an object, we have access to the methods, such as kill(), as you can see below.

PS D:\MyData\Portfolio\powershell> notepad
PS D:\MyData\Portfolio\powershell> notepad
PS D:\MyData\Portfolio\powershell> $npads = get-process -name notepad | sort-object -property id
PS D:\MyData\Portfolio\powershell> $npads

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
    238      14     3192      14236       0.14   4580   1 notepad
    238      13     3172      14204       0.08  14452   1 notepad


PS D:\MyData\Portfolio\powershell> $npads.kill()
PS D:\MyData\Portfolio\powershell>

I want to run get-process but I want to control how the output is displayed. I only want two columns. I want the name and the process Id. If I run get-process, I will get the following columns of data: Handles, NPM(K), PM(K), WS(K), CPU(s), Id, and SI ProcessName. In SQL you use the SELECT statement and you can use AS to change the name displayed on the output. Here is how we do this in PowerShell.

get-process | select-object -property name, @{name='procid';expression={$_.id}}
get-process | select-object -property name, @{name='Process Id';expression={$_.id}}
Series Navigation<< PowerShell ISEPowerShell Commandlets >>

Leave a Reply