- Powershell Commands & Plugins
- Getting Help
- Checking DNS Entries
- Execution Policy
- User Accounts and Sessions
- Windows Logs and Server Manager
- Quick Script to Connect to vCenters
- Connecting to ESX Hosts
- Host HBAs, Disks and SCSI Paths
- PowerShell Oneliners
- Snapshot a List of Servers
- List All IP Addresses
- Check Something on Multiple Linux VMs
- Other PowerShell and PowerCLI Commands
- Open a VM Console
- Example Script to Create a New VM
- Listing VMs
- VM, Host and Storage Cmdlets
- Increasing the Disk of a Linux VM
- Random Numbers and Services
- Keyboard Layout via PowerShell
- Active Directory Management
- Task Scheduler
- Other Commands
Powershell Commands and Plugins
Note: this section covers PowerShell, VMware PowerCLI and a few classic Windows CMD commands (
wmic,systeminfo,net user,query user,logoff,ipconfig), which also work from a PowerShell prompt. All hostnames, FQDNs, IP addresses, accounts and passwords are generic examples (example.com,esx01,vcenter01,admin.user, …): replace them with your own values.
Getting Help
List all the PowerShell commands containing “disk”.
Get-Command *disk*
Show the help of a command (equivalent of man on Linux). You can also use man $my_command.
Get-Help $my_command
Read the online documentation of a command.
Get-Help -Online $my_command
Show the history of the commands you ran previously.
Get-History
List the modules whose name matches an expression (here, containing “Directory”).
Get-Module *Directory*
List the properties and methods of an object (pipe an object into it).
Get-Member
Get information about a server (model, hypervisor, etc.) — CMD version.
wmic computersystem get model
Get detailed information about the system (OS, hotfixes, uptime, hardware, …) — CMD version.
systeminfo
Get the model and manufacturer of the computer — PowerShell version.
Get-WmiObject -Class Win32_ComputerSystem | Select-Object -Property Model, Manufacturer
Checking DNS Entries
Resolve a hostname (PowerShell equivalent of nslookup).
Resolve-DnsName example.com
Resolve a hostname and query a specific record type (here A).
Resolve-DnsName -Type A example.com
Query the TXT records of a hostname.
Resolve-DnsName txt.example.com -Type TXT
Execution Policy
Allow the current user to run local scripts (downloaded scripts must be signed).
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
User Accounts and Sessions
Show information about a domain user account (CMD). Replace USERNAME with the account name.
net user USERNAME /domain
List the users connected to the machine (short form: quser). Run it with an administrator account.
query user
List the users connected to a remote server.
query user /server:$SERVER
Log off a user. Get the user_ID from the output of query user, which lists the connected users.
logoff user_ID
Windows Logs and Server Manager
Open the Windows Event Viewer to read the Windows Server logs (or press Win + R, type eventvwr and press Enter).
eventvwr
Open Server Manager. It can be used as a central console to manage a whole Windows server fleet.
servermanager
Quick Script to Connect to vCenters
Small .ps1 script to quickly connect to several vCenters with PowerCLI.
Example with Two vCenters
Ignore invalid (self-signed) certificates for PowerCLI.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Ask for the credentials once and store them in a variable.
$Credentials = Get-Credential -Message "Enter credentials" -Username "DOMAIN\admin.user"
Connect to each vCenter with the stored credentials.
Connect-VIServer -Server 192.0.2.10 -Credential $Credentials
Connect-VIServer -Server 192.0.2.11 -Credential $Credentials
Example with Several vCenters
Ignore invalid certificates, and opt out of the VMware Customer Experience Improvement Program (CEIP) for the current user.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false -Confirm:$false
Ask for the credentials once and store them in a variable.
$Credentials = Get-Credential -Message "Enter credentials" -Username "admin.user@example.com"
Connect to each vCenter (one line per vCenter).
Connect-VIServer -Server vcenter01.example.com -Credential $Credentials
Connect-VIServer -Server vcenter02.example.com -Credential $Credentials
Connect-VIServer -Server vcenter03.example.com -Credential $Credentials
Connect-VIServer -Server vcenter04.us.example.com -Credential $Credentials
Connect-VIServer -Server vcenter05.asia.example.com -Credential $Credentials
Connect to Several vCenters with a Loop
Same result with a foreach loop over an array containing the FQDN of every vCenter. To add a vCenter, just add one line to the array. The try/catch block reports a failed connection without stopping the loop.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$Credentials = Get-Credential -Message "Enter credentials" -Username "admin.user@example.com"
$vCenters = @(
"vcenter01.example.com",
"vcenter02.example.com",
"vcenter03.example.com",
"vcenter04.us.example.com",
"vcenter05.asia.example.com"
)
foreach ($vCenter in $vCenters) {
try {
Connect-VIServer -Server $vCenter -Credential $Credentials -ErrorAction Stop | Out-Null
Write-Host "Connected to $vCenter"
}
catch {
Write-Error "Unable to connect to $vCenter : $_"
}
}
Check which vCenters you are currently connected to.
$global:DefaultVIServers | Select-Object Name, User, IsConnected
Connecting to ESX Hosts
Ignore invalid certificates for PowerCLI.
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect to the first vCenter with a username and a password stored in variables (prefer Get-Credential to avoid typing a password in clear text).
Connect-VIServer 192.0.2.10 -User $username -Password $password
Connect to the second vCenter.
Connect-VIServer 192.0.2.11 -User $username -Password $password
Connect to another vCenter using its hostname.
Connect-VIServer vcenter.example.com
Host HBAs, Disks and SCSI Paths
List the Fibre Channel HBAs of an ESX host.
Get-VMHostHba -VMHost esx02.example.com -Type FibreChannel
List the disks of an ESX host.
Get-VMHostDisk -VMHost esx02.example.com
Get the HBAs, the SCSI LUNs and the SCSI paths of an ESX host ($esxi is the host object, for example $esxi = Get-VMHost esx02.example.com).
$hbas = $esxi | Get-VMHostHba
$scsiluns = $hbas | Get-ScsiLun
$scsipaths = $scsiluns | Get-ScsiLunPath
PowerCLI Oneliners
The oneliners below read VM names from text files (one VM name per line): stop_vms.txt lists the VMs to shut down, start_vms.txt the VMs to start afterwards.
Print each VM name of the stop list (quick check that the file is read correctly).
foreach($VM in Get-Content F:\Scripts\stop_vms.txt){echo "$VM is present"}
Print each VM name of the start list.
foreach($VM in Get-Content F:\Scripts\start_vms.txt){echo "$VM is present"}
Gracefully shut down every VM of the stop list.
foreach($VM in Get-Content F:\Scripts\stop_vms.txt){echo "$VM is being stopped" ; Stop-VM -VM $VM -Confirm:$false}
Check that every VM of the start list exists in the vCenter.
foreach($VM in Get-Content F:\Scripts\start_vms.txt){Get-VM $VM}
Create a snapshot of every VM of the stop list that is powered off (here named after a change ticket).
foreach($VM in Get-Content F:\Scripts\stop_vms.txt){if ((Get-VM $VM).PowerState -eq "PoweredOff"){New-Snapshot -VM $VM -Name "CHG123456" -Quiesce}}
Check that the snapshot exists on every VM of the stop list.
foreach($VM in Get-Content F:\Scripts\stop_vms.txt){echo "$VM" ; get-vm $VM | Get-Snapshot -name "CHG123456";echo `n }
Start every VM of the start list that has the snapshot, waiting 60 seconds between each VM.
foreach($VM in Get-Content F:\Scripts\start_vms.txt){if (Get-Snapshot -VM $VM -Name "CHG123456"){Start-VM -VM $VM; Start-Sleep -Seconds 60}}
Remove all the snapshots of the VMs of the start list, without waiting for the task to complete (asynchronous).
foreach($VM in Get-Content F:\Scripts\start_vms.txt){Get-VM $VM | Get-Snapshot | Remove-Snapshot -RunAsync -Confirm:$false}
Remove all the snapshots of the VMs of the start list and wait for each task to complete.
foreach($VM in Get-Content F:\Scripts\start_vms.txt){Get-VM $VM | Get-Snapshot | Remove-Snapshot -Confirm:$false}
Snapshot a List of Servers
Create a snapshot of every powered-on VM listed in server_list.txt (one VM name per line).
foreach($VM in Get-Content .\server_list.txt){if ((Get-VM $VM).PowerState -eq "PoweredOn"){New-Snapshot -VM $VM -Name "Before Change" -Quiesce}}
List All IP Addresses
List the IP addresses of all the VMs of the vCenter.
Get-VM | Select-Object Name, @{Name="IP Address"; Expression={$_ | Get-VMGuest | Select-Object -ExpandProperty IPAddress}}
Check Something on Multiple Linux VMs
These commands use plink.exe (the command-line SSH client of PuTTY). Replace admin_user and the server names with your own.
Check that the check_mk agent is active on several Linux servers.
$servers = @("server01", "server02") ; $servers | ForEach-Object{plink.exe -ssh admin_user@$_ "systemctl is-active check_mk.service"}
Check that firewalld is active on several Linux servers. Passing the password on the command line is insecure: prefer SSH keys (-i key.ppk) when possible.
$servers = @("server01", "server02") ; $servers | ForEach-Object{plink.exe -ssh -l admin_user -pw $password $_ "systemctl is-active firewalld"}
Other PowerShell and PowerCLI Commands
General PowerShell cmdlets
Get information about commands and cmdlets.
Get-Help
Show all the available commands.
Get-Command
Get the system family and the OS display version of the computer.
Get-ComputerInfo -Property CsSystemFamily,OSDisplayVersion
List the running processes.
Get-Process
Stop a process.
Stop-Process
Show the services (and their state).
Get-Service
Start a service.
Start-Service
Stop a service.
Stop-Service
Change the properties of a service.
Set-Service
Get the event logs.
Get-EventLog
Clear the event logs.
Clear-EventLog
Get information about a file or a folder.
Get-Item
Modify a file or a folder.
Set-Item
Display the content of a file.
Get-Content
Write to a file.
Set-Content
Get information about Active Directory users.
Get-ADUser
Modify an Active Directory user.
Set-ADUser
Create a new file or folder.
New-Item
Delete a file or a folder.
Remove-Item
Open a VM Console
Open the console of a VM in full screen.
Open-VMConsoleWindow -VM "server01" -FullScreen
Same thing, passing the VM through the pipeline.
Get-VM server01 | Open-VMConsoleWindow -FullScreen
Example Script to Create a New VM
List all the possible GuestID values for the -GuestID parameter.
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier])
List the available hosts.
Get-VMHost
List the available datastores.
Get-Datastore
Create a new VM on the first vCenter: 2 vCPUs, 16 GB of memory, a 40 GB thin-provisioned disk and a RHEL 9 guest OS.
New-VM -Name "vm-int01" -Server "192.0.2.10" -VMHost "esx01.example.com" -Datastore "datastore01" -NumCpu 2 -MemoryGB 16 -DiskGB 40 -DiskStorageFormat Thin -NetworkName "VM Network" -GuestID rhel9_64Guest
Attach an installation ISO to the new VM, connected at power-on.
New-CDDrive -VM vm-int01 -ISOPath "[ISODatastore] ISO/Linux/RHEL/9/rhel-9.4-x86_64-boot.iso" -StartConnected
Add a second 200 GB thin-provisioned disk to the new VM.
New-HardDisk -VM vm-int01 -CapacityGB 200 -Datastore datastore01 -StorageFormat Thin
The same procedure on the second vCenter (repeat it for each VM you need to create).
New-VM -Name "vm-prd01" -Server "192.0.2.11" -VMHost "esx02.example.com" -Datastore "datastore02" -NumCpu 2 -MemoryGB 16 -DiskGB 40 -DiskStorageFormat Thin -NetworkName "VM Network" -GuestID rhel9_64Guest
New-CDDrive -VM vm-prd01 -ISOPath "[ISODatastore] ISO/Linux/RHEL/9/rhel-9.4-x86_64-boot.iso" -StartConnected
New-HardDisk -VM vm-prd01 -CapacityGB 200 -Datastore datastore02 -StorageFormat Thin
Delete a VM permanently (removes it from the disk too). This cannot be undone.
Remove-VM vm-int01 -DeletePermanently
Listing VMs
See also this tutorial: Lister les machines virtuelles avec PowerCLI (IT-Connect).
List the VMs whose name starts with “server”.
Get-VM server*
List the VMs located on a specific datastore.
Get-VM -Datastore datastore1
List the VMs of a specific vCenter.
Get-VM -Server "vcenter_name"
Example: list all the VMs of the vCenter 192.0.2.10.
Get-VM -Server "192.0.2.10"
Show on which ESX host a VM is located (plus its power state and hardware version).
Get-VM server01 | Select-Object Name,VMHost,PowerState,HardwareVersion
Show the datacenters of the vCenter (for instance DC1 and DC2).
Get-Datacenter
VM, Host and Storage Cmdlets
Get information about all the VMs of the vCenter.
Get-VM
Create a new virtual machine.
New-VM
Start a virtual machine.
Start-VM
Shut down a virtual machine.
Stop-VM
Restart a virtual machine.
Restart-VM
Get information about the ESXi hosts.
Get-VMHost
Add a new ESXi host.
New-VMHost
Modify the properties of a virtual machine (for example CPU or memory).
Set-VM
Show the datastores of the vCenter.
Get-Datastore
Create a new datastore.
New-Datastore
Get information about the ESXi clusters.
Get-Cluster
Show the resource pools available in a cluster.
Get-ResourcePool
Retrieve the storage policies.
Get-SpbmStoragePolicy
Show the storage details of an ESXi host.
Get-VMHostStorage
Get information about the SCSI LUNs.
Get-ScsiLun
Show the network adapters of a VM.
Get-NetworkAdapter -VM $VM
Modify the network settings of a VM.
Set-NetworkAdapter
Get information about the guest operating system of a VM.
Get-VMGuest
Show the snapshots of the VMs.
Get-Snapshot
Create a new snapshot of a VM.
New-Snapshot
Add a new disk to a VM.
New-HardDisk -VM $VM -Datastore "DS Name" -CapacityGB $size -StorageFormat Thin
Show the last 10 tasks and events (all types) of a VM.
Get-VIEvent -Entity $VMname -MaxSamples 10
Test the connectivity to a TCP port (equivalent of ping and netstat / telnet). Here: SSH port 22.
Test-NetConnection -ComputerName $IP -Port 22
Increasing the Disk of a Linux VM
Procedure to increase the disk of a Linux VM. First connect to the vCenters (see Quick Script to Connect to vCenters).
Check that the VM is found.
Get-VM $VMName
List the disks of the VM.
Get-HardDisk -VM vm_name
List the disks of the VM with their size, file name and ID, to get the name of the hard disk you want to increase.
Get-VM VMName | Get-HardDisk | Select Name,CapacityGB,FileName,Id
Increase “Hard disk 1” by giving it its new final size in GB (this resizes the VMDK).
Get-VM VMName | Get-HardDisk -Name "Hard disk 1" | Set-HardDisk -CapacityGB Final_Size
Then, on the Linux side, rescan the disk (see SCSI Devices) and grow the partition and the filesystem (see Disks and Partitions and LVM).
Random Numbers and Services
Generate a random number.
Get-Random
Show the status of all services, or of a specific service (alias: gsv).
Get-Service -Name servicename
Start a service.
Start-Service -Name servicename
Stop a service.
Stop-Service -Name servicename
Keyboard Layout via PowerShell
Add the US English language to the current language list.
$LanguageList = Get-WinUserLanguageList
$LanguageList.Add("en-US")
Set-WinUserLanguageList $LanguageList
Or: replace the language list with US English only.
Set-WinUserLanguageList -LanguageList "en-US" -Force
Or: create a language list with both the US keyboard and the US International keyboard (with dead keys).
$LangList = New-WinUserLanguageList en-US
$LangList[0].InputMethodTips.Add("0409:00000409") # US keyboard
$LangList[0].InputMethodTips.Add("0409:00020409") # US International (with dead keys)
Set-WinUserLanguageList $LangList -Force
To make sure the US International layout is the default one, use the Set-WinDefaultInputMethodOverride cmdlet with the appropriate input tip.
Set-WinDefaultInputMethodOverride -InputTip "0409:00020409"
Active Directory Management
Install the RSAT tools on Windows Server.
Install-WindowsFeature -Name RSAT-AD-PowerShell
Or on Windows 10/11.
Add-WindowsCapability -Online -Name Rsat.ActiveDirectory.DS-LDS.Tools~~~~0.0.1.0
Import the Active Directory module.
Import-Module ActiveDirectory
Unlock an account.
Unlock-ADAccount -Identity "jdoe"
Enable a disabled account.
Enable-ADAccount -Identity "jdoe"
Check the properties of an account (last password change, password expired, locked out).
Get-ADUser -Identity "firstname.lastname" -Properties PasswordLastSet, PasswordExpired, LockedOut
Reset the password, unlock the account and force a password change at next logon, all in one command.
Set-ADAccountPassword -Identity "jdoe" -Reset -NewPassword (ConvertTo-SecureString "TempPass123!" -AsPlainText -Force) -PassThru |
Unlock-ADAccount -PassThru |
Set-ADUser -ChangePasswordAtLogon $true
Change (reset) the password of a user.
Set-ADAccountPassword -Identity "username" -Reset -NewPassword (ConvertTo-SecureString "NewPassword123!" -AsPlainText -Force)
Force a password change at the next logon.
Set-ADUser -Identity "username" -ChangePasswordAtLogon $true
Prompt for the passwords. It asks for the old password, the new one and a confirmation.
Set-ADAccountPassword -Identity $username
Combined command: reset the password, force the user to change it at next logon and unlock the account.
Set-ADAccountPassword -Identity "firstname.lastname" -Reset -NewPassword (ConvertTo-SecureString "<TempPassword>" -AsPlainText -Force) -PassThru | Set-ADUser -ChangePasswordAtLogon $true -PassThru | Unlock-ADAccount
Specify the domain controller to use.
Set-ADAccountPassword -Identity "jdoe" -Server "dc01.example.local" -Reset -NewPassword (ConvertTo-SecureString "NewPassword!" -AsPlainText -Force)
Use administrator credentials.
$AdminCred = Get-Credential
Set-ADAccountPassword -Identity "jdoe" -Credential $AdminCred -Reset -NewPassword (ConvertTo-SecureString "NewPassword!" -AsPlainText -Force)
Note: passwords typed in clear text in a command are kept in the PowerShell history. Use temporary passwords only, and force a change at next logon.
Task Scheduler
List the scheduled tasks.
Get-ScheduledTask
Create a new scheduled task.
New-ScheduledTask
Other Commands
Run commands on a remote computer (IP address or hostname).
Invoke-Command -ComputerName "ip_or_hostname" -ScriptBlock { my_commands }
Print text to the console (equivalent of echo on Linux).
Write-Host "text"
Write an error message to the error stream (like echo, but the output goes to the error stream so it can be redirected separately).
Write-Error "error message"
Release the DHCP IP address.
ipconfig /release
Get a new IP address from DHCP.
ipconfig /renew