PS - Learning PowerShell 4.0 - Part 1


Here are a few fundamentals of PowerShell Version 4...

Installation Overview


  • Windows .NET Framework 4.5
  • Windows Managment Framework 4.0
  • (WFM 4.0 Includes - Powershell 4.0 (Shell & ISE))
  • (WFM 4.0 Includes - Powershell Web Service - ODATA Extension)
  • (WFM 4.0 Includes - Windows Remote Management - WinRM)
  • (WFM 4.0 Includes - Windows Management Instrmentation - WMI)
  • (WFM 4.0 Includes - Powershell Desired State Configuration - DSC)


(Update - Windows 2016 and Windows 10 runs PowerShell 5.x)





Check local PowerShell version

You can use the default $PSVerionTable to check the local version of PowerShell: 

$PSVersionTable

Execute commands on remote machines

You can use the Invoke (verb) and Command (noun) to run commands on remote machines, in the below example we use it to check the PowerShell version installed across 3 machines. (Remoting must be enabled)

#region " PowerShell Version Selected Computers "
 Invoke-Command -ComputerName COMPUTER1, COMPUTER2, COMPUTER3 -ScriptBlock {$PSVersionTable.PSVersion}
#endregion


Search for all computers in AD and run the command against these



#region " PowerShell Version All AD Computers "
Invoke-Command -ComputerName (Get-ADComputer -Filter * | Select-Object -expand Name) -ScriptBlock {$PSVersionTable.PSVersion}
#endregion 

Query WMI for OS and Service Pack

#region " OS Version & Service Pack Check "
Get-WmiObject -Class Win32_OperatingSystem | Format-Table Caption, ServicePackMajorVersion -AutoSize
#endregion


Query Registry for .NET 4.5

#region " .NET 4.5 Check "
(Get-ItemProperty -Path ‘HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full’ -ErrorAction SilentlyContinue).Version -like ‘4.5*’
#endregion 



Shutdown Script to install PowerShell 4.0 across the enterprise:

the following can be assigned via GPO as a shutdown script to install the required components for PowerShell 4.0.


@ECHO off
REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /f 4.5

IF %errorlevel%==1 GOTO INSTALLNET45
IF %errorlevel%==0 GOTO WMFCHECK

:INSTALLNET45

\installs\NDP452-KB2901907-x86-x64-AllOS-ENU.exe /q /norestart

:WMFCHECK

REG QUERY "HKLM\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine" /f 4.0

IF %errorlevel%==1 GOTO INSTALLWMF
IF %errorlevel%==0 GOTO EXIT

:INSTALLWMF

If /I "%processor_architecture%"=="x86" (GOTO 32bit) Else (GOTO 64bit)

:64bit
Start /wait wusa.exe "\\SERVER\SHARE\Windows6.1-KB2819745-x64-MultiPkg.msu" /quiet /norestart
Exit

:32bit
Start /wait wusa.exe "\\SERVER\SHARE\Windows6.1-KB2819745-x86-MultiPkg.msu" /quiet /norestart
Exit

:EXIT

Exit


Find PowerShell Alais, Cmdlet and Functions


#region "Find PowerShell Alais, Cmdlet, Functions and Applicaitons"
Get-Command
#endregion

Find PowerShell Alais, Cmdlet and Functions & Applications

#region "Find PowerShell Alais, Cmdlet, Functions and Applicaitons"
Get-Command *
#endregion

Find PowerShell Alais

#region "Find PowerShell Alais"
Get-Command -CommandType Alias
#endregion

Find cmdlets with Service in the name from the Active Directory Module

#region "Find cmdlets with Service in the name from the Active Directory Module"
Get-Command -Name *Service* -CommandType cmdlet -Module ActiveDirectory
#endregion


Find cmdlets with a verb of GET and a noun of SERVICE

#region "Find cmdlets with a verb of GET and a noun of SERVICE"
Get-Command -verb get -noun service
#endregion

Find cmdlets with a PARAMETERNAME of COMPUTERNAME

#region "Find cmdlets with a PARAMETERNAME of COMPUTERNAME"
Get-Command -ParameterName computername
#endregion

Display path environment variable contents

#region "display path environment varible"
$env:Path
#endregion


Help Commands

# updating help from the internet
Get-Help Update-Help -Examples
Update-Help

# saving & updating help locally
Save-Help -DestinationPath \\SERVER\SHARE\help
Update-Help -SourcePath \\SERVER\SHARE\help

# searching help
Get-Help *variable*
Get-Help about_automatic_variables -ShowWindow

# getting help
help Get-Service -Detailed #This give the parameters & examples, It also gives the positional value for named parameters.
help Get-Service -Examples #Gives Examples
help Get-Service -Full #The full help including params, examples, notes, input/output types
help Get-Service -Parameter d*

# using help
help Get-Service -ShowWindow
Get-Service bi* -ComputerName COMPUTER1, COMPUTER2 | Select-Object MachineName,Status, DisplayName | Format-List

help Start-Service -Online
Get-Service bi* -ComputerName COMPUTER1, COMPUTER2 | Start-Service #input object parameter set
Start-Service -DisplayName 'Background Intelligent Transfer Service' -COMPUTER1, COMPUTER2 #displayname parameter set
Start-Service bi* -ComputerName COMPUTER1, COMPUTER2 #default parameter set

Parameters


Positional Parameter [-Name] (These mean you do not need to actually supply them as they are default, if there are more than one then you have to supply in the correct position.)
Mandatory Parameters -Examples these do not have brackets around them and are required.
Switch Parameters [-Full] are parameters without a data type, these are simply on or off, running the command without them by default leaves them off.
Parameters with [] with the string, for example [-Category<String[]>]  means that the parameter accepts an array for input such as COMPUTER1, COMPUTER2, ETC

SYNTAX
    Get-Help [[-Name] <String[]>] -Examples [-Category<String[]>] [-Component <String>[][>] [-Full] [-Functionality <String[]>] [-Path <String>] [-Role <String[]>] [<CommonParameters>]

Optional Parameters are where you have brackets around both the name and the data type, example:

SYNTAX
    Get-Help [[-Name] <String[]>] [-Category<String[]>] [-Component <String>[][>] [-Full] [-Functionality <String[]>] [-Path <String>] [-Role <String[]>] [<CommonParameters>]


-ShowWindow

You can use the -ShowWindow switch to open the topic into a new window for example:




Out-GridView


You can use the pipeline to Output all or selected properties to the Out-GridView cmdlet to display properties, which you can then sort copy and paste, etc.

    Get-Service | Sort-Object DisplayName | Select * | Out-GridView
Or
    GSV | Sort DisplayName | Select * | Out-GridView




Comments