I decided to create a summary of the useful powershell commands and tricks to give the beginners a starting point to prepare and customize the powershell workspace. I don’t want to copy the powershell help. I will add here only the commands I use to customize powershell environment on the computers I’m working on.

For XenApp 6.5 specific powershell commands see: XenApp 6.5 powershell commands cheat sheet

For XenDesktop 7.6 specific powershell commands see: XenDesktop 7.6 powershell commands cheat sheet

Examine existing powershell environment  

#
Get-Host or (Get-Host).Version
or
$PSVersionTable.PSVersion
#
#list modules that have been imported into the current session.
Get-Module

#list modules that are installed and can be imported into the current session.
Get-Module -ListAvailable
#

Configure powershell environment on new computer

#Check if profile is configured
Test-path $profile

#Create a new profile file
New-item –type file –force $profile

#edit profile to add required commands etc
Notepad $profile
#
#
Set-ExecutionPolicy RemoteSigned #available options: Restricted, AllSigned, RemoteSigned, Unrestricted 
#
#list modules that have been imported into the current session.
Get-Module

#list modules that are installed and can be imported into the current session.
Get-Module -ListAvailable
#

Change command output view settings  

#Standard view
Get-Service a*

#Display output with full column width 
Get-Service a* |Format-Table -AutoSize
#

Manage remote computer

# Enable remote management on target computer
Enable-PSRemoting -Force

#Connect to remote computer Server01 with current credentials
Enter-PSSession -ComputerName Server01 

#Connect to remote computer Server01 with different credentials
Enter-PSSession -ComputerName Server01 -Credential Admin

#Execute command on remote computers
Invoke-Command -ComputerName Server01,Server02 -ScriptBlock { Get-Service IMA* } -Credential Admin

#Execute local script on remote computers
Invoke-Command -computername Server05 -filepath .\xa65_load.ps1
#
#
PS C:\> Enter-PSSession -ComputerName Server03
[Server03]: PS C:\>
[Server03]: PS C:\> Restart-Computer

or

Invoke-Command -ComputerName Server03 -ScriptBlock { Restart-Computer }
#
#Display session info  
query session /server:Server01
or
QWinSta session /server:Server01
#
#
#Reset/logoff session on remote server
reset session 2 /server:Server01
or
logoff 4 /server:Server01
or
rwinsta 5 /server:Server01
#

 

Manage local computer

#
#To display basic system information 
Get-CimInstance CIM_ComputerSystem

#To display hardware information  
Get-CimInstance CIM_BIOSElement
#
#
#-LogName available options: System, Application, Security etc (use TAB to display more options)
#-EntryType available options: Error, Warning, Information, FailureAudit, SuccessAudit 

# List 3 newest errors from System eventlog
Get-EventLog -LogName System -EntryType Error -newest 3

# List System eventlog errors from last 2 days (today and day before)
Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddDays(-1)

# List System eventlog errors from last 2 hours
Get-EventLog -LogName System -EntryType Error -After (Get-Date).AddHours(-2)

# List System eventlog errors reported by source: MetaFrame
Get-EventLog -LogName System -EntryType Error -Source "MetaFrame"

# List System eventlog errors with selected EventID
Get-EventLog -LogName System -EntryType Error -InstanceId 9017

# Display details of selected eventlog entry
Get-EventLog -LogName System -EntryType Error -Index 83203

# Display details of selected eventlog entry with full description
Get-EventLog -LogName System -EntryType Error -Index 83203 |Format-List
#
# 
#By default, all comparison operators are case-insensitive.
# 
#To make a comparison operator case-sensitive, precede the operator name with a "c" (-ceq, -clt, etc..)

#Available operators:
-eq	       #Equal to
-lt	       #Less than
-gt	       #Greater than
-ge	       #Greater than or Eqaul to
-le	       #Less than or Equal to
-ne	       #Not equal to
-Like
-NotLike
-Match
-NotMatch
-Contains
-NotContains
-In
-NotIn
-Replace
#
get-wmiobject -namespace "root\CIMV2" -class "Win32_Printer"  

# For use only on Server 2012  and Windows 8 with RSAT installed 
Get-Printer -ComputerName PrintServer01
#
# Registry location for installed printers
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows x64\Drivers\Version-3
#
#
get-wmiobject  -namespace "root\CIMV2" -class "Win32_Printer" |select systemname -Unique |where {$_.systemName -ne $env:computername}
#
# Display update status
Get-MpComputerStatus | select *updated, *version

#Start signature update
Update-MpSignature

#Start computer scan
Start-MpScan -ScanType quick #Availabale options: FullScan, QuickScan, CustomScan
#
#List all scheduled jobs
Get-ScheduledJob

#Create new scheduled job
$t=New-JobTrigger -AtStartup -RandomDelay 45

Register-ScheduledJob -Name "Windows Defender Siganture Update" -FilePath "c:\windows\Citrix24\Scripts\Defender update.ps1" -Trigger $t
#
# List trigger details 
Get-JobTrigger -Name *Defender*

# Create new job trigger
# Available parameters for time schedule: At, Once, Daily, Weekly, AtStartup, At LogOn
# Available paremeters for repeating action: DaysInterval, WeeksInterval, RepetitionInterval
New-JobTrigger -Once -At "1/20/2012 3:00 AM"
or
New-JobTrigger -Daily -At "4:15 AM" -DaysInterval 3
or
New-JobTrigger -Weekly -DaysOfWeek Monday, Wednesday, Friday -At "23:00" -WeeksInterval 4
or
New-JobTrigger -Weekly -DaysOfWeek 1,3,5 -At 23:01 -WeeksInterval 4

#Add new job trigger to an existing scheduled job
Add-JobTrigger -Name "Windows Defender Siganture Update" -Trigger (New-JobTrigger -Daily -At 3:10AM)
#
#Mount iso
$iso=Mount-DiskImage -PassThru \\nas\iso\W2K8R2.iso
or 
$iso=Mount-DiskImage -PassThru \\nas\iso\W2K8R2.iso,\\nas\iso\XD76.iso 

#List details for mounted iso
Get-Volume -DiskImage $iso
or
foreach($1 in $iso){Get-Volume -DiskImage $1}  

#Dismount iso 
Get-Volume e |Get-DiskImage |Dismount-DiskImage
or
Dismount-DiskImage $iso.ImagePath
#
#Display service list - basic information only
Get-Service
or 
Get-WmiObject Win32_Service
 
#Display service list - detailed information 
Get-Service | select *
or 
Get-WmiObject win32_service | select *

#Display service account information 
Get-WmiObject win32_service |select Name, DisplayName, StartName

#Display service account information for SQL Server services 
Get-WmiObject win32_service |select Name, DisplayName, StartName |where {$_.name -match "sql"}
#

 

Active Directory

#List basic info
Get-ADUser UserABC

#List basic info + selected properties
Get-ADUser UserABC -Properties CN, Description

#List all info
Get-ADUser UserABC -Properties *
#

#List all groups with the specified name
Get-ADGroup -filter {Name -like "Group_name"}
#

Hyper-V

#Verify if Hyper-V role isn't already enabled.
Get-WindowsFeature Hyper-V*

#Install Hyper-V role with powershell cmdlets
Install-WindowsFeature Hyper-V, Hyper-V-PowerShell –Restart
#
#List available Hyper-V commands
Get-Command –Module Hyper-V

#Import server manager
Import-Module ServerManager

#Install module
Add-WindowsFeature Hyper-V-PowerShell

#List available Hyper-V commands
Get-Command –Module Hyper-V

#Update help files
Update-Help 
#

Miscellaneous commands and tricks

#Use gwmi or Get-WmiObject

#List available namespaces 
gwmi -namespace root -class "__Namespace" | Select Name 

#List classes for particular namespace
gwmi -namespace root\citrix -list 

#More detailed listing for classes 
gwmi -namespace root\citrix -list |where {$_.name -like "metaframe*"} 
#
# Get Start Time
$startDTM = (Get-Date)

{command }

# Get End Time
$endDTM = (Get-Date)

# Echo Time elapsed
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
#