{"id":789,"date":"2012-07-19T10:28:37","date_gmt":"2012-07-19T17:28:37","guid":{"rendered":"http:\/\/jim-zimmerman.com\/?p=789"},"modified":"2017-06-27T17:56:14","modified_gmt":"2017-06-28T00:56:14","slug":"powershell-notes","status":"publish","type":"post","link":"https:\/\/jim-zimmerman.com\/?p=789","title":{"rendered":"PowerShell notes."},"content":{"rendered":"<p>Check execution policy to allow you to run PowerShell scripts: <\/p>\n<p>Get-ExecutionPolicy<br \/>\nTo allow yourself to execute powershell script:<br \/>\nSet-ExecutionPolicy RemoteSigned<br \/>\nor<br \/>\nSet-ExecutionPolicy AllSigned<br \/>\nor<br \/>\nSet-ExecutionPolicy Unrestricted<\/p>\n<p>Display all users login script:<br \/>\nPS> Get-Aduser -Filter * -Properties ScriptPath<\/p>\n<p>Map a network drive using PowerShell:<br \/>\n(New-Object -com WScript.Network).MapNetworkDrive(&#8220;Y:&#8221;,&#8221;\\\\servername\\sharename&#8221;)<\/p>\n<p>Help New-PSDrive -full<\/p>\n<p>Manage PowerShell drives:<br \/>\nGet-PSDrive<\/p>\n<p>PS C:\\Scripts> Get-PSDrive<\/p>\n<p>Name           Used (GB)     Free (GB) Provider      Root<br \/>\n&#8212;-           &#8212;&#8212;&#8212;     &#8212;&#8212;&#8212; &#8212;&#8212;&#8211;      &#8212;-<br \/>\nAlias                                  Alias<br \/>\nC                  45.88        187.01 FileSystem    C:\\<br \/>\ncert                                   Certificate   \\<br \/>\nEnv                                    Environment<br \/>\nFunction                               Function<br \/>\nG                1113.88        282.94 FileSystem    G:\\<br \/>\nHKCU                                   Registry      HKEY_CURRENT_USER<br \/>\nHKLM                                   Registry      HKEY_LOCAL_MACHINE<br \/>\nI                1113.88        282.94 FileSystem    I:\\<br \/>\nS                1113.88        282.94 FileSystem    S:\\<br \/>\nU                 302.15         47.85 FileSystem    U:\\<br \/>\nV                1113.88        282.94 FileSystem    V:\\<br \/>\nVariable                               Variable<br \/>\nWSMan                                  WSMan<br \/>\nY                 592.14        431.73 FileSystem    Y:\\<br \/>\nZ                 592.14        431.73 FileSystem    Z:\\<\/p>\n<p>This only remove drives available in the PowerShell environment.<\/p>\n<p>Remove-PSDrive -Name name<br \/>\nPS C:\\Scripts> Remove-PSDrive -Name Y<\/p>\n<p>To determine the PowerShell version:<br \/>\nPS H:\\> $Host.Version<\/p>\n<p><strong>Hyper-V Related Commands<\/strong><br \/>\nTo display all VMs:<br \/>\nPS> get-VM<\/p>\n<p>To shutdown all VMs:<br \/>\nPS> get-VM | stop-VM<\/p>\n<p>Or, shutdown one VM:<br \/>\nPS> stop-VM -Name <em>VMNAME<\/em><\/p>\n<p>You can even use an * wildcard.<br \/>\nThis will only stop all VMs with CANADA in the name:<br \/>\nPS> stop-VM -Name &#8216;*CANADA*&#8217;<\/p>\n<p>To export all VMs:<br \/>\nPS> export-VM <em>VMNAME<\/em> -Path &#8216;<em>EXPORTDIRECTORY<\/em>&#8216;<\/p>\n<p>Example:<br \/>\nPS> Export-VM VMWIN2012 -Path &#8216;D:\\Exports&#8217;<\/p>\n<p>Or, you can use the following to export all your VMs ( After you stop them, of course.):<br \/>\nPS> Get-VM | Export-VM -Path &#8216;<em>EXPORTDIRECTORY<\/em>&#8216;<\/p>\n<p>To import\/restore a VM:<br \/>\nPS C:\\> Import-VM -Path &#8216;<em>XMLfile<\/em>&#8216;<\/p>\n<p>Example:<br \/>\nPS E:\\>  Import-VM -Path &#8216;E:\\MYVMs\\Virtual Machines\\5FBF3F53-0A55-4124-883E-6F149A1E731E.XML&#8217;<\/p>\n<p>To start a VM:<br \/>\nPS> Start-VM -Name <em>VMNAME<\/em><\/p>\n<p>Wildcard to start as well:<br \/>\nPS> start-VM -Name &#8216;*CANADA*&#8217;<\/p>\n<p>Access information about files and directories:<br \/>\nThis will display the file\/directory name and what has access to it:<br \/>\nPS> Get-Childitem -LiteralPath <em>somedirectory<\/em> -Recurse | Get-Acl | Format-List -Property PSPath,AccessToString<\/p>\n<p>To list all the Properties, use:<br \/>\n   -Property *<\/p>\n<p>Another way to determine disk size and free space:<br \/>\nPS> Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer | Select-Object DeviceID,Size,FreeSpace<\/p>\n<p>To determine physical memory installed:<br \/>\nPS> Get-WmiObject CIM_PhysicalMemory<\/p>\n<p>To determine processor installed:<br \/>\nPS> Get-WmiObject CIM_Processor<\/p>\n<p>To list local user accounts on a machine:<br \/>\nPS> Get-WmiObject -Class Win32_UserAccount -Filter  &#8220;LocalAccount=&#8217;True'&#8221; -Computername <em>computername<\/em><\/p>\n<p>Send email:<br \/>\nPS> Send-MailMessage -to <em>ToEmailAddress<\/em> -Subject &#8220;<em>SomeSubject<\/em>&#8221; -body &#8220;<em>BodyTest<\/em>&#8221; -smtpserver <em>EmailServer<\/em> -from <em>FromEmailAddress<\/em><\/p>\n<p>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:\\&#8230;\\Email.ps1.  Works great.  You could associate ps1 with powershell and forgo the powershell command as well.<\/p>\n<p>Get a list of computers from AD:<br \/>\nThis will display all computers in AD that begin with the letter A:<br \/>\nPS> Get-ADComputer -Filter &#8216;samAccountName -like &#8220;A*$&#8221;&#8216; | Select Name<\/p>\n<p>This will give you a list of all computers and their operating system, version and service pack level:<br \/>\nPS> Get-ADComputer -Filter &#8216;samAccountName -like &#8220;*$&#8221;&#8216; -Property * | Format-Table Name,OperatingSystem,OperatingSystemServicePack,OperatingSystemVersion<\/p>\n<p>PowerShell to access a remote machine&#8217;s registry:<br \/>\nNote: I did this on a Windows 10 machine.  Everything needed to be done from an elevated PowerShell prompt.<br \/>\nYou need to download and install the PSRemoteRegistry powershell module from https:\/\/psremoteregistry.codeplex.com\/.<br \/>\nOnce installed, you need to import the module:<br \/>\nPS> Import-Module PSRemoteRegistry<\/p>\n<p>PS> Get-RegValue -ComputerName <em>COMPUTERNAME<\/em> -Key &#8220;<em>KEYNAME<\/em>&#8221; -Value <em>VALUENAME<\/em><\/p>\n<p>Here is an example.  This will pull the value from the LOCAL MACHINE hive:<br \/>\nPS> Get-RegValue -ComputerName COMPUTERNAME -Key &#8220;SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon&#8221; -Value CachedLogonsCount<br \/>\nSet a Value on a remote computer using the same PSRemoteRegistry module:<br \/>\nPS> Set-RegString -Computer <em>COMPUTERNAME<\/em> -Key &#8220;SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon&#8221; -Value CachedLogonsCount -Data &#8220;0&#8221;<\/p>\n<p>How to find all LockedOut ADS accounts:<br \/>\nPS> Get-ADUser -LDAPFilter &#8220;(objectClass=User)&#8221; -Properties Name,LockedOut | Select Name,LockedOut | findstr &#8220;True&#8221;<\/p>\n<p>To unlock and account:<br \/>\nPS> Unlock-ADAccount -Identity <em>SamAccountName<\/em><\/p>\n<p>Unlock script sample:<\/p>\n<blockquote><p>CLS<br \/>\n$logfile = &#8220;\\\\<em>servername<\/em>\\C$\\Tools\\UnlockAccounts-$(Get-Date -Format `&#8221;yyyyMMdd`&#8221;).log&#8221;<br \/>\nfunction MyLog($somestring)<br \/>\n{<br \/>\n   $somestring | Out-File -Filepath $logfile -append<br \/>\n}<br \/>\nWrite-Host &#8220;Below is a list of currently locked out accounts:&#8221;<br \/>\nGet-AdUser -LDAPFilter &#8220;(objectClass=User)&#8221;  -Properties SamAccountName,LockedOut | Select SamAccountName,Lockedout | findstr &#8220;True&#8221;<br \/>\n$Acct = Read-Host -Prompt &#8216;Which account would you like to unlock&#8217;<br \/>\nIf ($Acct)<br \/>\n{<br \/>\n  Write-Host &#8220;Attempting to unlock &#8216;$Acct'&#8221;<br \/>\n  Unlock-ADAccount -Identity $Acct<br \/>\n  MyLog &#8220;$(Get-Date -Format `&#8221;MM\/dd\/yyyy hh:mm:ss tt`&#8221;) Unlock attempted for account: $Acct.&#8221;j<br \/>\n}<br \/>\nElse<br \/>\n{<br \/>\n  Write-Host &#8220;No account was entered.&#8221;<br \/>\n}<br \/>\nPAUSE<\/p><\/blockquote>\n<p>To list the file permissions of all files and directories under a directory:<\/p>\n<p>PS> Get-ChildItem -Recurse <em>directory<\/em> | Get-Acl | Format-List<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(&#8220;Y:&#8221;,&#8221;\\\\servername\\sharename&#8221;) Help New-PSDrive -full Manage PowerShell drives: Get-PSDrive PS C:\\Scripts> [&#038;hellip<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[238,293],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-documentation","tag-hyper-v","tag-powershell"],"share_on_mastodon":{"url":"","error":""},"_links":{"self":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=789"}],"version-history":[{"count":25,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":1559,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=\/wp\/v2\/posts\/789\/revisions\/1559"}],"wp:attachment":[{"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jim-zimmerman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}