PowerShell: Finding Domain Controllers

If your computer is logged on to an Active Directory, here is some code to get to your domain controllers. Note that this will raise errors if you are currently not logged on to a domain. PS> $Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() PS> $Domain.DomainControllers This lists all domain controllers. To find just one, try this: $Domain.FindDomainController() And…

Continue Reading

PowerShell: Comparing Services

Comparing Services Compare-Object is one of the most widely ignored most powerful cmdlet around. It can compare results and figure out differences. For example, if you’d like to know the differences in service configuration between two machines, here’s the simple solution: $pc1 = Get-Service -ComputerName server1 $pc2 = Get-Service -ComputerName server2 Compare-Object $pc1 $pc2 -Property…

Continue Reading

PowerShell: Getting Windows Product Key

Ever wanted to read out the Windows license key? In the Windows Registry, this key is present, but it is stored as a digital ID. To convert it back to the license key used to originally license the product, try this function. It uses some serious math: function Get-ProductKey { $map=”BCDFGHJKMPQRTVWXY2346789″ $value = (get-itemproperty “HKLM:\SOFTWARE\Microsoft\Windows…

Continue Reading

PowerShell: Admin Privileges Enabled?

If you want to know whether your script has currently full Administrator privileges, here is an (admittedly long) one-liner that tells you: (New-Object System.Security.Principal.WindowsPrincipal([System.Security.Principal. WindowsIdentity]::GetCurrent())).IsInRole([System.Security.Principal. WindowsBuiltInRole]::Administrator) Try executing this line in a regular PowerShell and then in an elevated shell, and check out the difference. Or, create your own console prompt which turns red when…

Continue Reading