When you are using Powershell, it really helps if you use special characters to write your own scripts or interpret someone else’s scripts. Some of them are really common and get used very often….

# Hash – Single line comment

 

#This script is for ....
#This variable is to ...

 

$ Dollar sign – is used to declare a variable

$ComputerName = "Server1"
$password = "p@sw0rd"

 

| Pipeline- Executes the left side and with  the output feeds to the right

 

Get-Process | Select -first 10

 

% Percentage – Short for “FOREACH”

 

% ($Server in $Servers) { Write-Host $_}

 

? Question Mark – Short for “Where”

 

Get-process | ? {$_.name -like 'win*' -and $_.status -eq 'Running'}

 

@ () – Declares an array

 

$Servers = @ ("server1", "server2", "server3")

 

@ {} – Declares an hash table

 

$servers = @{"server1" = "Dell";
             "server2" = "HP";
             "server3" = "Nutanix"}

$servers["server3"]
Nutanix

------------------------------------ OR

$params = @{};
$params['class'] = 'Win32_DiskDrive';
$params['filter'] = 'size=256052966400'; #find a drive which is 256GB in size
		  
Get-WmiObject @params
Get-WmiObject -Class 'Win32_DiskDrive' -Filter 'size=256052966400'

 

& Ampersand – Executes strings as commandlets

 

& "Get-Scheduledjob"

 

! Exclamation – Short for “not”

$serverName = $null;
if(!$serverName) { Write-Host '$a is null' }

 

:: Double colon – Reference static member of a class. The class name must be enclosed in square brackets.

[string]::Equals = ("Computers", "COMPUTERS")
False # this will compare these two strings and returns false

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s