Archive | May, 2012

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: Parsing Date and Time

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

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: Map Network Drive

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

Continue Reading →

PowerShell: Sharing Folders

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

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: Catching Errors

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

Continue Reading →

PowerShell: Using Shared Variables

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

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 →
revia in alcoholism