Tag Archive: msonline

Powershell 101

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







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. 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

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:






AzureAD PowerShell module


Note: the preview module is the most recent and contain new cmdlet like Get-AzureADDirectorySetting or Set-AzureADDirectorySetting

Download and install the 64-bit version of the Online Services Sign-In Assistant from the following url: http://www.microsoft.com/en-us/download/details.aspx?id=41950

Download and install the 64-bit version of the Windows Azure Active Directory Module for Windows PowerShell the following url: https://bposast.vo.msecnd.net/MSOPMW/9031.1/amd64/AdministrationConfig-en.msi



Getting started with PowerShell for O365:


Azure AD PowerShell module:



Powershell cmdlets reference and tips/scripts:




How to install the module to access Office 365/Azure AD:

The PowerShellGet module requires PowerShell 3.0 or newer. Therefore, PowerShellGet requires one of the following operating systems:

  • Windows 10
  • Windows 8.1 Pro
  • Windows 8.1 Enterprise
  • Windows 7 SP1
  • Windows Server 2016 TP5
  • Windows Server 2012 R2
  • Windows Server 2008 R2 SP1

PowerShellGet also requires .NET Framework 4.5 or above. You can install .NET Framework 4.5 or above from here.

Then from a PowerShell prompt enter:

Check installed module:
Get-InstalledModule -Name “AzureAD*”

Get-InstalledModule -Name “MSOnline”

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

At the message about an untrusted repository, type Y. It will take a minute or so for the new module to install.




example:  get-azureaduser -filter “displayname eq ‘firstname lastname'” | select displayname,mail

To list all user attributes:
get-msoluser -userprincipalname firstname.lastname@mydomain.com | select *

With AzureAD cmdlets (need module AzureAD):
get-azureaduser -filter “displayname eq ‘firstname lastname'” | select *

Working with extension attributs:


Working with Azure AD Extension Attributes with Azure AD PowerShell v2