The aim of this post is to explain some basic tips and tricks about the usage of powershell in a real world:

For reference to well-known PowerShell web sites, go to my Web links on this web site or here is a reference for powershell cmdlets: http://ss64.com/ps/

else in French a simple tutorial:http://lavalisedutechnicien.free.fr/joomla/index.php?option=com_content&view=article&id=195&Itemid=297

Careful : PS v3 is available with the latest version of Win8 or Winsrv 2012.

0-to retrieve the version and language of powershell:

$host.version

$PSCulture    or Get-Culture

1- create powershell profiles:

get-help about_profiles   ,  get-help about_*

the two well-known profiles are:

  • $profile        ; to display the path of the current user’s (Current User; Current Host)

NCEDALBERA3\C:\Windows\system32> $profile
C:\Users\jdalbera\My DATA\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

  • $pshome ;  to display the path of the main profile.ps1 (All Users;All Hosts)

NCEDALBERA3\C:\Windows\system32> $pshome
C:\Windows\System32\WindowsPowerShell\v1.0

if there is no profile.ps1 create it !

NCEDALBERA3\C:\Windows\system32> cd C:\Windows\System32\WindowsPowerShell\v1.0
NCEDALBERA3\C:\Windows\System32\WindowsPowerShell\v1.0> dir *.ps1
Répertoire : C:\Windows\System32\WindowsPowerShell\v1.0

Mode                LastWriteTime     Length Name
—-                ————-     —— —-
-a—        04/11/2012     23:13        802 Microsoft.PowerShellISE_profile.ps1
-a—        13/03/2014     22:53       1774 profile.ps1

  • Microsoft.PowerShellISE_profile.ps1  ; correspond to the powershell profile read when ISE is started

 

2- import the modules on profiles (for Windows server):

servermanager, activedirectory

to list the modules available: get-module -list

3- verify the variables (variables psdrive)

get-psdrives  ; to list all available PSdrives

dir variables:

and modify the error;debug… accordingly

4- Powershell help:

PS v3: be careful with the get-help command. http://www.silogix.fr/blog-silogix/powershell-v3-du-nouveau-dans-laide.aspx

– use/read intensively the equivalent of well-knows MAN pages on Unix world, called here, get-help and get-help about_*

– get-help <cmdlet> -examples|-full|-detailed

– <cmdlet> | get-member  ;  to list the cmdlet properties and methods

– get-command -module <modulename>

– get-command *module

– get-command | where-object { $_.definition -like “*-new*”}  ; display all the cmdlets containing -newest as parameter

– get-command | where-object { $_.definition -like “*computername*”}  ; display all the cmdlets containing “computername” as parameter

– get-module -listavailable

– get-alias                ; list all the aliases

– <cmdlet> | fl prop1,*prop2… or | ft -autosize

5- On powershell scripts:

– create functions and modules; function(){}

– use try/catch/finally, to trap errors

– use -ErrorActivation or -ErrorVariable for each cmdlet

– use the foreach-object {} cmdlet

– use the import-CSV, export-CSV, exportto-XML, out-file

– use intensively: where-object {} cmdlet

– use intensively: tee-object -filepath|-variable

– use intensively arrays: get-help about_arrays, about_hash_tables

– WMI support: get-wmiobject cmdlet

5- Examples of well-known cmdlets:

– to restart a computer without be prompted: restart-computer

– get-childitem -path c:\users -recurse -include *.txt

– to rename files in bulk mode: get-ChildItem -Filter “*current*” -Recurse | Rename-Item -NewName {$_.name -replace ‘current’,’old’ }

– to rename image files in bulk mode: Get-ChildItem -Filter *.jpg | %{ Rename-Item $_.FullName ($_.Name.split(“.”)[0].Substring(3)+”.jpg”)}

– get-service | format-list  ; get-service | get-member |out-gridview

– get-process | format-wide