PowerShell: Finding Domain Controllers

Posted Leave a commentPosted in PowerShell

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 […]

PowerShell: Executing Commands in Groups

Posted Leave a commentPosted in PowerShell

In traditional batch files, you can use && to execute a second command only if the first one worked. In PowerShell, the same can be achieved by using the try/catch construct. You just need to know some little tricks. Take a look at this: try { $ErrorActionPreference=’Stop’ # commands follow } catch {} Now, if […]

PowerShell: Parsing Date and Time

Posted 18 CommentsPosted in PowerShell

Parsing Date and Time Parsing a date and/or time information is tricky because formatting depends on the regional settings. This is why PowerShell can convert date and time based on your regional settings or in a culture-neutral format. Let’s assume this date: PS> $date = ‘1/6/2013’ If you convert this to a datetime type, PowerShell […]

PowerShell: Comparing Services

Posted Leave a commentPosted in PowerShell

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 […]

PowerShell: Map Network Drive

Posted 1 CommentPosted in PowerShell

Sure you can use the command net use to map a network drive. But this would not check for existing mapped drives. Here’s a small function that first checks to see that the URL you are mapping to does not yet exist, avoiding duplicate mapped drives: function New-MapDrive { param($Path) $present = @(Get-WmiObject Win32_Networkconnection | […]

PowerShell: Sharing Folders

Posted Leave a commentPosted in PowerShell

Console commands are first class PowerShell citizens, so sometimes it may be easier to use classic console commands to solve a problem. Here is a function that creates a local folder and also shares it so others can use it via network. Just be aware that the net.exe used to share the folder requires you […]

PowerShell: Getting Windows Product Key

Posted 2 CommentsPosted in PowerShell

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 […]

PowerShell: Catching Errors

Posted 1 CommentPosted in PowerShell

In forums, people often get confused with error handling. For example, this code does not call the error handler. Instead, the red PowerShell error message pops up: try { Remove-Item \\$name\c$\windows\temp\filename.exe } catch {Write “Not able to access files on $name”} When you want to catch errors produced by cmdlets, always make sure you add […]

PowerShell: Using Shared Variables

Posted 2 CommentsPosted in PowerShell

By default, all variables created in functions are local, so they only exist within that function and all functions that are called from within this function. Sometimes, you’d like to examine variables defined in a function after that function executed. Or you’d like to persist a variable, so next time the function is called it […]

PowerShell: Admin Privileges Enabled?

Posted Leave a commentPosted in PowerShell

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 […]