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
Videos:
https://channel9.msdn.com/Blogs/Taste-of-Premier/PowerShellBasicsPart1
https://channel9.msdn.com/Blogs/Taste-of-Premier/PowerShell-Basics-Part-3-Real-Word-Script-Examples
https://channel9.msdn.com/Blogs/Taste-of-Premier/Whats-New-in-PowerShell-v5
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
Customize the powershell profile:
https://www.howtogeek.com/50236/customizing-your-powershell-profile/
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
or
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 1.1.183.8
Get-InstalledModule -Name “MSOnline”
8– Parsing text with powershell:
https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-1-3/
https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-2-3/
https://devblogs.microsoft.com/powershell/parsing-text-with-powershell-3-3/