Check execution policy to allow you to run PowerShell scripts:
Get-ExecutionPolicy
To allow yourself to execute powershell script:
Set-ExecutionPolicy RemoteSigned
or
Set-ExecutionPolicy AllSigned
or
Set-ExecutionPolicy Unrestricted
Display all users login script:
PS> Get-Aduser -Filter * -Properties ScriptPath
Map a network drive using PowerShell:
(New-Object -com WScript.Network).MapNetworkDrive(“Y:”,”\\servername\sharename”)
Help New-PSDrive -full
Manage PowerShell drives:
Get-PSDrive
PS C:\Scripts> Get-PSDrive
Name Used (GB) Free (GB) Provider Root
—- ——— ——— ——– —-
Alias Alias
C 45.88 187.01 FileSystem C:\
cert Certificate \
Env Environment
Function Function
G 1113.88 282.94 FileSystem G:\
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
I 1113.88 282.94 FileSystem I:\
S 1113.88 282.94 FileSystem S:\
U 302.15 47.85 FileSystem U:\
V 1113.88 282.94 FileSystem V:\
Variable Variable
WSMan WSMan
Y 592.14 431.73 FileSystem Y:\
Z 592.14 431.73 FileSystem Z:\
This only remove drives available in the PowerShell environment.
Remove-PSDrive -Name name
PS C:\Scripts> Remove-PSDrive -Name Y
To determine the PowerShell version:
PS H:\> $Host.Version
Hyper-V Related Commands
To display all VMs:
PS> get-VM
To shutdown all VMs:
PS> get-VM | stop-VM
Or, shutdown one VM:
PS> stop-VM -Name VMNAME
You can even use an * wildcard.
This will only stop all VMs with CANADA in the name:
PS> stop-VM -Name ‘*CANADA*’
To export all VMs:
PS> export-VM VMNAME -Path ‘EXPORTDIRECTORY‘
Example:
PS> Export-VM VMWIN2012 -Path ‘D:\Exports’
Or, you can use the following to export all your VMs ( After you stop them, of course.):
PS> Get-VM | Export-VM -Path ‘EXPORTDIRECTORY‘
To import/restore a VM:
PS C:\> Import-VM -Path ‘XMLfile‘
Example:
PS E:\> Import-VM -Path ‘E:\MYVMs\Virtual Machines\5FBF3F53-0A55-4124-883E-6F149A1E731E.XML’
To start a VM:
PS> Start-VM -Name VMNAME
Wildcard to start as well:
PS> start-VM -Name ‘*CANADA*’
Access information about files and directories:
This will display the file/directory name and what has access to it:
PS> Get-Childitem -LiteralPath somedirectory -Recurse | Get-Acl | Format-List -Property PSPath,AccessToString
To list all the Properties, use:
-Property *
Another way to determine disk size and free space:
PS> Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer | Select-Object DeviceID,Size,FreeSpace
To determine physical memory installed:
PS> Get-WmiObject CIM_PhysicalMemory
To determine processor installed:
PS> Get-WmiObject CIM_Processor
To list local user accounts on a machine:
PS> Get-WmiObject -Class Win32_UserAccount -Filter “LocalAccount=’True'” -Computername computername
Send email:
PS> Send-MailMessage -to ToEmailAddress -Subject “SomeSubject” -body “BodyTest” -smtpserver EmailServer -from FromEmailAddress
To run from a script or Task Scheduler, put the command in a ps1 file like Email.ps1, then in the script put: powershell c:\…\Email.ps1. Works great. You could associate ps1 with powershell and forgo the powershell command as well.
Get a list of computers from AD:
This will display all computers in AD that begin with the letter A:
PS> Get-ADComputer -Filter ‘samAccountName -like “A*$”‘ | Select Name
This will give you a list of all computers and their operating system, version and service pack level:
PS> Get-ADComputer -Filter ‘samAccountName -like “*$”‘ -Property * | Format-Table Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion
PowerShell to access a remote machine’s registry:
Note: I did this on a Windows 10 machine. Everything needed to be done from an elevated PowerShell prompt.
You need to download and install the PSRemoteRegistry powershell module from https://psremoteregistry.codeplex.com/.
Once installed, you need to import the module:
PS> Import-Module PSRemoteRegistry
PS> Get-RegValue -ComputerName COMPUTERNAME -Key “KEYNAME” -Value VALUENAME
Here is an example. This will pull the value from the LOCAL MACHINE hive:
PS> Get-RegValue -ComputerName COMPUTERNAME -Key “SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon” -Value CachedLogonsCount
Set a Value on a remote computer using the same PSRemoteRegistry module:
PS> Set-RegString -Computer COMPUTERNAME -Key “SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon” -Value CachedLogonsCount -Data “0”
How to find all LockedOut ADS accounts:
PS> Get-ADUser -LDAPFilter “(objectClass=User)” -Properties Name,LockedOut | Select Name,LockedOut | findstr “True”
To unlock and account:
PS> Unlock-ADAccount -Identity SamAccountName
Unlock script sample:
CLS
$logfile = “\\servername\C$\Tools\UnlockAccounts-$(Get-Date -Format `”yyyyMMdd`”).log”
function MyLog($somestring)
{
$somestring | Out-File -Filepath $logfile -append
}
Write-Host “Below is a list of currently locked out accounts:”
Get-AdUser -LDAPFilter “(objectClass=User)” -Properties SamAccountName,LockedOut | Select SamAccountName,Lockedout | findstr “True”
$Acct = Read-Host -Prompt ‘Which account would you like to unlock’
If ($Acct)
{
Write-Host “Attempting to unlock ‘$Acct'”
Unlock-ADAccount -Identity $Acct
MyLog “$(Get-Date -Format `”MM/dd/yyyy hh:mm:ss tt`”) Unlock attempted for account: $Acct.”j
}
Else
{
Write-Host “No account was entered.”
}
PAUSE
To list the file permissions of all files and directories under a directory:
PS> Get-ChildItem -Recurse directory | Get-Acl | Format-List