IT tutorials
 
Technology
 

Windows Server 2008 : Starting and Using PowerShell - Understanding PowerShell Variables

12/26/2013 1:05:45 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

Variables are units in memory that hold different values. Both built-in and user-defined variables are available in PowerShell.

Note

All PowerShell variables start with the dollar sign ($).


The following table shows some of the commonly used variables built-in to Windows PowerShell.

Built-in VariablesComments
$pshome
PS C:\> cd $pshome
PS C:\Windows\System32\
WindowsPowerShell\v1.0>

Location of PowerShell command and associated configuration files. A profile can be placed here. The example changes the directory to the location of $pshome.
$profile
PS C:\> $profile
C:\Users\Darril\Documents\
WindowsPowerShell\Microsoft.
PowerShell_profile.ps1
PS C:\> test-path $profile

Shows the perceived location of the profile.ps1 file. This path does not exist by default. The test-path $profile command returns false if it does not exist and true if the file does exist.
$errorAn array of all the errors from the current session. The most recent error can be retrieved with $error[0] (the first item in the array).
$_.

PS C:\> get-service | where
{$_.status -eq "stopped" }

The $_. string is a special variable that indicates the current cmdlet being piped. In the example, the get-service command retrieves all services and is then piped to the where clause. The $_. variable is used with dot notation to identify the status of each service and see if they are stopped.

Note

This effectively works like a foreach clause. It lists the information for each service that matches the status of stopped.

$pwdCurrent working directory.

Note

You can retrieve a full list of variables with the get-variable cmdlet or the variable alias. The output lists the variables and their current values.


You can also create your own variables. Variables are assigned with the equal sign (=) and prefixed with the dollar sign ($). You can then use the variable in the current session or within a script.

The following table shows the methods used to assign and manipulate variables.

Assigning VariablesComments
PS C:\>  $d = get-date
PS C:\> $d

Assigns the current date and time value to $d. You can view the value of the variable by entering $d by itself.
PS C:\> $counter = 0

Assigns the value 0 to a variable.
PS C:\> $counter =
$counter + 1
PS C:\> $counter++
PS C:\> $counter

Increments a variable by 1. If the $counter started as 0, the first line would add 1 to it, and the second line would add 1 again. The third line would show the value is 2.
PS C:\> $counter =
$counter - 1
PS C:\> $counter--
PS C:\> $counter

Decrements a variable by 1. If the $counter started as 0, the first line would subtract 1 from it and the second line would subtract 1 again. The third line would show the value is -2.
PS C:\> $msg =
"Success!"
PS C:\> $msg

Assigns a string of characters to a variable. You can view the value of the variable by entering $msb by itself.

When working with numeric variables, you can use different mathematical assignment values, as shown in the following table.

Working with Numeric VariablesComments (Including the Value $x if it Starts with 10)
=
PS C:\> $x = 10

Assignment. The value of $x is 10 after this command.
+
PS C:\> $x = $x + 5

Addition. If $x started at 10, the value of $x would be 15 after this command.
-
PS C:\> $x = $x − 5

Subtraction. If $x started at 10, the value of $x would be 5 after this command.
*
PS C:\> $x = $x * 5

Multiplication. If $x started at 10, the value of $x is 50 after this command.
/
PS C:\> $x = $x / 5

Division. If $x started at 10, the value of $x is 2 after this command.
%
PS C:\> $x = $x % 3

Module (remainder). If $x started at 10, the value of $x is 1 after this command.
+=
PS C:\> $x += $x

Additive assignment. If $x started at 10, the value of $x is 20.
-+
PS C:\> $x -= $x

Subtractive assignment. If $x started at 10, the value of $x is 0 after this command.
*=
PS C:\> $x *= 10

Multiplicative assignment. If $x started at 10, the value of $x is 100 after this command.
/=
PS C:\> $x /= 2

Quotient assignment. If $x started at 10, the value of $x is 1 after this command.
%=
PS C:\> $x /= 3

Remainder assignment. If $x started at 10, the value of $x is 1 after this command.
++
PS C:\> $x ++

Increment. If $x started at 10, the value of $x is 11 after this command.
--
PS C:\> $x --

Decrement. If $x started at 10, the value of $x is 9 after this comm
 
Others
 
- Windows Server 2008 : Starting and Using PowerShell - Redirecting Output with Windows PowerShell, Understanding PowerShell Errors
- Windows Server 2008 : Starting and Using PowerShell - Exploring get-member
- Windows Server 2008 : Starting and Using PowerShell - Creating Aliases, Discovering Windows PowerShell Commands
- Exchange Server 2010 : Managing Mailbox Databases (part 2) - Properties of a Mailbox Database
- Exchange Server 2010 : Managing Mailbox Databases (part 1) - Viewing Mailbox Databases, Creating Mailbox Databases
- Exchange Server 2010 : Mailbox Storage - Determining the Number of Databases, Allocating Disk Drives
- Exchange Server 2010 : Getting to Know Exchange Database Storage (part 2)
- Exchange Server 2010 : Getting to Know Exchange Database Storage (part 1)
- Understanding SharePoint 2013 authentication (part 3) - Understanding app authentication flow in SharePoint 2013
- Understanding SharePoint 2013 authentication (part 2) - Understanding how SharePoint 2013 authenticates apps
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us