SlideShare a Scribd company logo
Windows PowerShell
Sandun Perera
Geveo Australasia
30th May 2012
 Command line interpreter
 Unix shell
 Apple script
 MS-DOS
 Windows Script Host
 Windows PowerShell
 Administrative tasks
 Routine tasks
 Maintenance tasks
Shell scripting
 1. Install Microsoft .NET Framework 2.0
 2. Install Microsoft .NET Framework 3.5.1.
 Required for Windows PowerShell Integrated Scripting
Environment (ISE) and Out-GridView.
 3. Install WinRM 2.0 CTP3. (Windows Remote Shell)
 This is required if you want to take advantage of the remoting and
back- ground jobs features.
 4. Install Windows PowerShell 2.
Starting point (ps2)
Integrated Scripting Environment
(ISE)
 Set Execution Policy
 Customise Console
 Profile editing
 Profile using
Permission and Profiling
Cmdlets
Windows
PowerShell
(Cmdlet)
Windows PowerShell
(Alias)
cmd.exe /
COMMAND.COM
(MS-DOS, Windows,
OS/2, etc.)
Description
Get-Location gl, pwd cd Display the current directory/present working directory.
Set-Location sl, cd, chdir cd, chdir Change the current directory
Clear-Host cls, clear cls Clear the screen
Copy-Item cpi, copy, cp copy Copy one or several files / a whole directory tree
Get-Help help, man help Help on commands
Remove-Item ri, del, erase, rmdir, rd, rm del, erase, rmdir, rd Delete a file / a directory
Rename-Item rni, ren ren, rename Rename a file / a directory
Move-Item mi, move, mv move Move a file / a directory to a new location
Get-ChildItem gci, dir, ls dir List all files / directories in the (current) directory
Write-Output echo, write echo Print strings, variables etc. to standard output
Pop-Location popd popd
Change the current directory to the directory most recently
pushed onto the stack
Push-Location pushd pushd Push the current directory onto the stack
Set-Variable sv, set set Set the value of a variable / create a variable
 Boolean: True or false condition.
 Byte: An 8-bit unsigned whole number from 0 to 255, such as 32.
 Char: A 16-bit unsigned whole number from 0 to 65,535. For example, 1,026.
 Date: A calendar date, such as January 1, 2009.
 Decimal: A 128-bit decimal value, such as 3.14159265.
 Double: A double-precision 64-bit floating point number. In effect, this is another kind of
decimal value but has a narrower range of values than a decimal.
 Integer: A 32-bit signed whole number from –2,147,483,648 to 2,147,483,647, such as 152 or –
1839.
 Long: A 64-bit signed whole number. This is like an integer but holds far more values, such as
9,233,372,036,854,775,807.
 Object: Any kind of object.
 Short: A 16-bit unsigned whole number. This is like an integer but holds far fewer values. It
can only hold values from –32,768 to 32,767.
 Single: A single-precision 32-bit floating point number. This is like a double but holds far
fewer values, such as 20.3654.
 String: A grouping of characters that most people just call text.
Data types
 $MyVariable
 $AVariableWithANumber6
 ${A variable with spaces}
 ${A variable with special characters @#%%}
Variables
 $Error: An array of error objects representing the most recent errors.
 $Host: Current host application for Windows PowerShell.
 $Profile: Stores the full path to the Windows PowerShell user profile for
the default shell.
 $PID: Contains the process identifier of the Windows PowerShell
process.
 $PSUICulture: Stores the name of the UI culture currently in use.
 $NULL: Contains NULL or empty value.
 $False: Contains FALSE.
 $True: Contains TRUE.
Automatic variables
 Concatenating
 Escape characters
 Multiline string
 String functions
String handling
 Backslash: 
 Dollar sign: $
 Dot: .
 Pipe: |
 Question mark: ?
 Star: *
 Plus sign: +
 Open square bracket: [
 Open parenthesis: (
 Close parenthesis: )
 Caret: ^
Regular expressions
 Create functions
 Calling functions
 Passing arguments
 Return values
Functions
Operator Meaning
-eq Equal
-ne Not equal
-ine Not equal; not case sensitive (new in Windows PowerShell 2)
-lt Less than
-le Less than or equal
-gt Greater than
-ge Greater than or equal
Operators
 if statement
 switch statement
 for loop
 foreach loop
 while loop
 do while loop
 do until loop
Control flow statements
 Add (adds a Timespan value)
 AddDays
 AddHours
 AddMilliseconds
 AddMinutes
 AddMonths
 AddSeconds
 AddTicks
 AddYears
DateTime
 Arrays
 Hash tables
 Access elements
Collections
 Reading files
 Writing file
 Create files
 Delete files
 Copy / Rename / Move files
File handling
 Access system information
 Disk info
 Services
 Event log
 BIOS
Windows Management
Infrastructure (WMI)
 Connect to SQL Server
 Create SQL Command
 Execute and capture data
 Display data
SQL Query Execution
 Count errors
 Throw exception
 Handle exception
 Write to log file
 Write to Event log
Exception handling
 Message box
 Installed drivers info
 Path shrink
 Convert to PDF
 Send email
 Hello Anna
 Text to audio recording
Cool stuff
 Not allowing scripts to execute
 Improperly using commas to separate parameters when
calling a function
 Not defining functions before using them
 Forgetting that pipelines pass objects and not just strings
 Not casting variables as a string
 Making incorrect comparisons
 Expending the pipeline to far
 Not taking variable scope in account
 Debugging without the debugger
 Forgetting to use available .NET classes
Common Mistakes
 Windows PowerShell™ 2 For Dummies® by Steve Seguis
 http://powershell.com/cs/
 http://en.wikipedia.org/wiki/Windows_PowerShell
 http://sandunangelo.blogspot.com/2010/02/introduction-to-
powershell-scripting.html
References
Thank you!

More Related Content

Windows PowerShell

  • 1. Windows PowerShell Sandun Perera Geveo Australasia 30th May 2012
  • 2.  Command line interpreter  Unix shell  Apple script  MS-DOS  Windows Script Host  Windows PowerShell  Administrative tasks  Routine tasks  Maintenance tasks Shell scripting
  • 3.  1. Install Microsoft .NET Framework 2.0  2. Install Microsoft .NET Framework 3.5.1.  Required for Windows PowerShell Integrated Scripting Environment (ISE) and Out-GridView.  3. Install WinRM 2.0 CTP3. (Windows Remote Shell)  This is required if you want to take advantage of the remoting and back- ground jobs features.  4. Install Windows PowerShell 2. Starting point (ps2)
  • 5.  Set Execution Policy  Customise Console  Profile editing  Profile using Permission and Profiling
  • 6. Cmdlets Windows PowerShell (Cmdlet) Windows PowerShell (Alias) cmd.exe / COMMAND.COM (MS-DOS, Windows, OS/2, etc.) Description Get-Location gl, pwd cd Display the current directory/present working directory. Set-Location sl, cd, chdir cd, chdir Change the current directory Clear-Host cls, clear cls Clear the screen Copy-Item cpi, copy, cp copy Copy one or several files / a whole directory tree Get-Help help, man help Help on commands Remove-Item ri, del, erase, rmdir, rd, rm del, erase, rmdir, rd Delete a file / a directory Rename-Item rni, ren ren, rename Rename a file / a directory Move-Item mi, move, mv move Move a file / a directory to a new location Get-ChildItem gci, dir, ls dir List all files / directories in the (current) directory Write-Output echo, write echo Print strings, variables etc. to standard output Pop-Location popd popd Change the current directory to the directory most recently pushed onto the stack Push-Location pushd pushd Push the current directory onto the stack Set-Variable sv, set set Set the value of a variable / create a variable
  • 7.  Boolean: True or false condition.  Byte: An 8-bit unsigned whole number from 0 to 255, such as 32.  Char: A 16-bit unsigned whole number from 0 to 65,535. For example, 1,026.  Date: A calendar date, such as January 1, 2009.  Decimal: A 128-bit decimal value, such as 3.14159265.  Double: A double-precision 64-bit floating point number. In effect, this is another kind of decimal value but has a narrower range of values than a decimal.  Integer: A 32-bit signed whole number from –2,147,483,648 to 2,147,483,647, such as 152 or – 1839.  Long: A 64-bit signed whole number. This is like an integer but holds far more values, such as 9,233,372,036,854,775,807.  Object: Any kind of object.  Short: A 16-bit unsigned whole number. This is like an integer but holds far fewer values. It can only hold values from –32,768 to 32,767.  Single: A single-precision 32-bit floating point number. This is like a double but holds far fewer values, such as 20.3654.  String: A grouping of characters that most people just call text. Data types
  • 8.  $MyVariable  $AVariableWithANumber6  ${A variable with spaces}  ${A variable with special characters @#%%} Variables
  • 9.  $Error: An array of error objects representing the most recent errors.  $Host: Current host application for Windows PowerShell.  $Profile: Stores the full path to the Windows PowerShell user profile for the default shell.  $PID: Contains the process identifier of the Windows PowerShell process.  $PSUICulture: Stores the name of the UI culture currently in use.  $NULL: Contains NULL or empty value.  $False: Contains FALSE.  $True: Contains TRUE. Automatic variables
  • 10.  Concatenating  Escape characters  Multiline string  String functions String handling
  • 11.  Backslash:  Dollar sign: $  Dot: .  Pipe: |  Question mark: ?  Star: *  Plus sign: +  Open square bracket: [  Open parenthesis: (  Close parenthesis: )  Caret: ^ Regular expressions
  • 12.  Create functions  Calling functions  Passing arguments  Return values Functions
  • 13. Operator Meaning -eq Equal -ne Not equal -ine Not equal; not case sensitive (new in Windows PowerShell 2) -lt Less than -le Less than or equal -gt Greater than -ge Greater than or equal Operators
  • 14.  if statement  switch statement  for loop  foreach loop  while loop  do while loop  do until loop Control flow statements
  • 15.  Add (adds a Timespan value)  AddDays  AddHours  AddMilliseconds  AddMinutes  AddMonths  AddSeconds  AddTicks  AddYears DateTime
  • 16.  Arrays  Hash tables  Access elements Collections
  • 17.  Reading files  Writing file  Create files  Delete files  Copy / Rename / Move files File handling
  • 18.  Access system information  Disk info  Services  Event log  BIOS Windows Management Infrastructure (WMI)
  • 19.  Connect to SQL Server  Create SQL Command  Execute and capture data  Display data SQL Query Execution
  • 20.  Count errors  Throw exception  Handle exception  Write to log file  Write to Event log Exception handling
  • 21.  Message box  Installed drivers info  Path shrink  Convert to PDF  Send email  Hello Anna  Text to audio recording Cool stuff
  • 22.  Not allowing scripts to execute  Improperly using commas to separate parameters when calling a function  Not defining functions before using them  Forgetting that pipelines pass objects and not just strings  Not casting variables as a string  Making incorrect comparisons  Expending the pipeline to far  Not taking variable scope in account  Debugging without the debugger  Forgetting to use available .NET classes Common Mistakes
  • 23.  Windows PowerShell™ 2 For Dummies® by Steve Seguis  http://powershell.com/cs/  http://en.wikipedia.org/wiki/Windows_PowerShell  http://sandunangelo.blogspot.com/2010/02/introduction-to- powershell-scripting.html References

Editor's Notes

  1. Script pane Output pane Command pane