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:

else in French a simple tutorial:



0-to retrieve the version and language of powershell:


$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

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  or get-installedmodules

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.

– 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 {$ -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

6- How to remove string in a variable:

PS C:\WINDOWS\system32> $grp = “o365grp-my group-jda”

PS C:\WINDOWS\system32> $res = $grp -creplace ‘^o365grp-‘,”
PS C:\WINDOWS\system32> $res
my group-jda

7- How to install AzureAD and MSOnline modules:

Get-InstalledModule -Name “AzureAD*”

To uninstall a previous version of AzureADPreview or AzureAD, run this command:
Uninstall-Module AzureADPreview
Uninstall-Module AzureAD

To install the latest version of AzureADPreview, run this command:   Install-Module AzureADPreview -Force

To install MSOnline run: Install-Module Msonline -Force

To install MSonline latest version: Install-Module -Name MSOnline -RequiredVersion

Get-InstalledModule -Name “MSOnline”

8– Parsing text with powershell: