Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Along the same lines of the question about autohotkey scripts: http://superuser.com/questions/7271/, just now it's about Powershell.

Any usefull, handy or powefull scripts. Please describe what they do. Feel free to include scripts that don't have anything to do with server jobs or sysadmin tasks.

For example:

This command will recursively move your *.jpg images into the current directory, and add the originally containing directory name as a prefix for the image file.

(ls -r -include *.jpg) | % { mv -literal $_ $_.Name.Insert(0, [String]::Format("{0} - ", $_.Directory.Name))}

Or something you can run on text documents:

$badString = "This        is         not        the way   it   should     be."
$badString #for debug only
while ($badString.Contains("  ")){
    $badString = $badString -replace "  "," "
    }
$badString #tada

Because Powershell is the future.

share|improve this question

closed as not constructive by 8088, Simon Sheehan, Wuffers, Sathya Oct 28 '11 at 6:31

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

2  
It would be better if you made this Community Wiki. You could also post your suggestions as answers to get the ball rolling. –  ChrisF Jan 4 '10 at 22:02
    
made it into a community wiki –  KdgDev Jan 5 '10 at 0:34

3 Answers 3

up vote 1 down vote accepted

Get-RecentUpdates.ps1, this will list the recent (default: last 7 days) updates from Microsoft Update (it is somewhat work in progress, some of the formatting should be moved to a .xmlps1 format definition):

#requires -Version 2.0
# Copyright Richard J Cox 2009. Use freely at your own risk

param([switch]$RawDisplay,
      [DateTime]$After = $([datetime]::Today.AddDays(-7)),
      [string]$Computer = '')

$extraArgs = @{}
if ($Computer.Length -gt 0) {
    $extraArgs.Computer = $Computer
}

$events = get-eventlog -After $after -logname system -InstanceId 19 -source "Microsoft-Windows-WindowsUpdateClient" @extraArgs | 
          select-Object -property EventId, Index, Source, TimeGenerated,
                        @{n='Message';e={$_.ReplacementStrings | Select-Object -first 1}}


if ($rawDisplay) {
    $events
} else {
    $events | ft -a -wrap Index, TimeGenerated, Message
}
share|improve this answer
    
That is usefull. Nice one. –  KdgDev Jan 5 '10 at 15:48

My favourite from here... gets the Windows serial key in its real form!..

function Get-WindowsKey {
    ## function to retrieve the Windows Product Key from any PC
    ## by Jakob Bindslet (jakob@bindslet.dk)
    param ($targets = ".")
    $hklm = 2147483650
    $regPath = "Software\Microsoft\Windows NT\CurrentVersion"
    $regValue = "DigitalProductId"
    Foreach ($target in $targets) {
        $productKey = $null
        $win32os = $null
        $wmi = [WMIClass]"\\$target\root\default:stdRegProv"
        $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
        $binArray = ($data.uValue)[52..66]
        $charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9"
        ## decrypt base24 encoded binary data
        For ($i = 24; $i -ge 0; $i--) {
            $k = 0
            For ($j = 14; $j -ge 0; $j--) {
                $k = $k * 256 -bxor $binArray[$j]
                $binArray[$j] = [math]::truncate($k / 24)
                $k = $k % 24
            }
            $productKey = $charsArray[$k] + $productKey
            If (($i % 5 -eq 0) -and ($i -ne 0)) {
                $productKey = "-" + $productKey
            }
        }
        $win32os = Get-WmiObject Win32_OperatingSystem -computer $target
        $obj = New-Object Object
        $obj | Add-Member Noteproperty Computer -value $target
        $obj | Add-Member Noteproperty Caption -value $win32os.Caption
        $obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
        $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
        $obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
        $obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
        $obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
        $obj | Add-Member Noteproperty ProductKey -value $productkey
        $obj
    }
}

Then use -

Get-WindowsKey

or even -

Get-WindowsKey "pc1", "pc2", "server999", "server777"

share|improve this answer
    
Nice one. Tried and works. –  KdgDev Jan 5 '10 at 0:35

My profile script - sources a shared directory with a bunch of useful scripts:

$SharedScripts = '\\FileServer\Share\PowerShell\Scripts' 
Get-ChildItem "$SharedScripts\*.ps1" | % {  
  . $_ 
  Write-Host ('Loaded shared library: ' + [System.IO.Path]::GetFileNameWithoutExtension($_)) 
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.