PowerShell
Handy Powershell
Divert errors to a file:
| |
| |
Open another powershell window as admin:
| |
Get location of exe running proces:
| |
Delete contents of folder
| |
Change to environment locations:
| |
| |
Show all environment vairables:
| |
Show path to PS modules:
$env:PSProfilepath
Add To PATH
| |
Restart computer remotely:
| |
Send a message to a user on a remote host:
msg /server: /v
Powershell Remoting
Connect to remote powershell session:
| |
If you are getting an error when remoting like “WinRM cannot process the request.” use Windows PowerShell to add each server to the Trusted Hosts list on your management computer:
| |
Note: the trusted hosts list supports wildcards, like Server*
To view your Trusted Hosts list:
| |
To empty the list:
| |
If errors show run the following command to check on the winrm service+config:
| |
##Services with Powershell Get services running on computer and display in a pauseable list:
| |
| |
Output Command History to text file:
Get-History | ForEach-Object { $_.CommandLine } > $env.USERPROFILE\testoutput.txt
Get Powershell to display all output in the case that output is displayed truncated:
Pipe to “out-string -width 500” to display in a sting of set number of characters:
| |
In the case of an array change the vairable $FormatEnumerationLimit to -1
| |
Script to ping IP address and log time and status of ping:
https://github.com/AleksPish/NetworkPingTest/blob/master/NetworkDownTest.ps1
Download file from internet:
| |
Also can use Download method of WebClient
| |
Get public IP address of device:
| |
Add Exclusions to security check from downloaded programs:
add-mppreference -exclusionpath ""
Get members of ad group:
get-adgroupmember -identity "" | select-object name
Get Computer / Server Uptime - last boot time
| |
Powershell Modules and comms errors
Install PS module
| |
If there is an error the issue may be with TLS - run the following command first:
| |
Resolve TLS problems for good by updating PowershellGet:
| |
| |
File Admin Powershell
List folders
| |
Move all files of a specified extension from the current directory to another directory, move recursively
| |
Display errors that were seen when accessing files:
| |
Active Directory Powershell
Export details of users in a specific OU:
| |
Get all groups a user is assigned to
| |
Get group:
| |
Get members of group:
| |
Change password expiry setting on ad accounts by OU Import-Module ActiveDirectory
| |
Search for adusers using powershell:
Can be used with various options: DistinguishedName, Enabled, GivenName, Name, ObjectClass, Object GUID, SamAccountName, SID, Surname, UserPrincipalName.
| |
Unlock user account:
| |
Check for lock status:
| |
Local Accounts commands
Use for managing local accounts:
| |
Change details of local user
| |
Change password:
| |
| |
| |
Add to group:
| |
Powershell for Admin
Get powershell update
| |
Get FSMO roles on which domain controllers at domain level:
Get-ADDomain | Select-Object InfrastructureMaster,PDCEmulator,RIDMaster | Format-List
Get FSMO roles on which domain controllers at forest level:
Get-ADForest | Select-Object DomainNamingMaster,SchemaMaster | Format-List
Get all current logged in sessions:
| |
Installing packages in powershell
Use winget to install packages:
| |
You may need to specify the source:
| |
Upgrade/update packages with winget:
| |
Chocolatey is now pretty much depreciated with the introduction of winget - install with MS store
Can use chocolatey to get packages:
| |
For SSH connections:
Putty:
choco install putty
OpenSSH:
choco install openssh # installs open ssh refreshenv # reloads the environment variables ssh remoteClient -i “MyKeyPair.pem” # connects to remoteClient via ssh
poshSSH:
Install-Module Posh-SSH # installs the posh-ssh module Get-Command -Module Posh-SSH # shows all posh-ssh commandlets New-SSHSession myclient -KeyFile “c:\data\MyKeyPair.pem” # connect to my client with the give keyfile Invoke-SSHCommandStream “ifconfig” -SessionId 0 # send ifconfig to the ssh session with id 0 Invoke-SSHCommand -SessionId 0 -Command “ifconfig” # send ifconfig to the ssh session with id 0 Invoke-SSHCommand -SessionId 0 -Command “logout” # send logout to the ssh session with id 0 Remove-SSHSession 0 # removes and closes the ssh session
For firefox:
choco install firefox -y
Issue with psrepository
try to fix psrepository:
| |
If this fails use the following:
Install the PSRepository using the following settings:
$Repository = @{ Name = ‘PSGallery’ SourceLocation = ‘https://www.powershellgallery.com/api/v2/' PublishLocation = ‘https://www.powershellgallery.com/api/v2/package/' ScriptSourceLocation = ‘https://www.powershellgallery.com/api/v2/items/psscript' ScriptPublishLocation = ‘https://www.powershellgallery.com/api/v2/package/' InstallationPolicy = ‘Untrusted’ }
Register-PSRepository @Repository
Powershell Alias
gsv Get-Service
spsv Stop-Service
sasv Start-Service
Powershell for Services and Processes
Get all properties of a service and display specific properties of the service:
| |
| |
Display list of only running services:
| |
Remotely Check Service:
| |
Get Service PID to kill process:
| |
| |
Get top 10 processes by memory usage
| |
-First 10
Get User Process with an active GUI (no background processes will be displayed:
| |
Encrypt Passwords for use in Powershell scripts - scheduled tasks
Use Export-Clixml
Easiest way is to export the user credentials as an xml object using export-clixml then import with import-clixml:
| |
Then import the user credentials from the exported file (the credentials are stored encrypted in the xml file):
| |
Use convertfrom/to-securestring method
Use the convertfrom-securestring command to take a secure string (password) then store as a file eg:
| |
| |
To use the the passwords in a script use the get-content:
| |
| |
| |
If you want to encrypt the username and password you can do the following:
| |
| |
| |
They are stored in separate files
If you want to get the password back as plain text you can use the following:
| |
Script Writing Info
Find out what escape character to use for special characters:
| |