Monday, 1 December 2008

Useful PowerShell snippets (part 1)

Some things I’m finding useful:

Processes running on a machine

Get-WmiObject Win32_Process -ComputerName mymachine | Select { $_.Name }

Currently logged in user on a machine

Get-WmiObject Win32_ComputerSystem -ComputerName MyMachine | Select { $_.UserName }

Does a machine have .NET 3.5 SP1 installed

function Get-Net35SP1([string] $machineName)
{
    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $machineName)
    $regKey= $reg.OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" )
    if ($regKey -ne $null) {
        Write-Output $regKey.GetValue("SP", 0)
        $regKey.Close()
    } else {
        Write-Output "Doesn't have .NET 3.5"
    }
    $reg.Close()
}

2 comments:

Rob Farley said...

Can't you just use Get-Process to fetch the processes running? That way you have a bunch more members available. Try:

Get-Process | gm

Rob

David Gardiner said...

that works nicely for the local computer, but there doesn't appear to be an option to tell it to run against a remote machine.

-dave