In celebration of Friday the 13th and to help ward off any triskaidekaphobia I thought I'd offer up 13 PowerShell scriptblocks. These are scriptblocks that might solve a legitimate business need like finding how long a server has been running to the more mercurial such as how many hours before I can go home.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
A scriptblock is any small piece of PowerShell code enclosed in a set of curly braces. I say small, but there's really no limit. You can even pass parameters to a script block. In this way they are like functions but without the function key word. Save the script block to a variable and when you want to execute, use the invoke operator, the & character. For example, here's one of my luck 13 script blocks.
[cc lang="powershell"]
#1. Get running services
$running={Param ([string]$computername=$env:computername) get-service -computername $computername |
where {$_.status -eq "Running"}}
[/cc]
To invoke it:
[cc lang="powershell"]
PS C:\> &$running
[/cc]
Because this particular script block takes a computername as a parameter I could also do this:
[cc lang="powershell"]
PS C:\> &$running Server01
[/cc]
I won't post the complete script here, but these are the goodies:
#1. Get running services
#2. Get logical disk information
#3. Get day of the year
#4. Get top services by workingset size
#5. Get OS information
#6. Get system uptime
#7. get a random number between 1 and 13
#8. How old are you?
#9. get %TEMP% folder stats
#10. Get event log information
#11. Get local admin password age in days
#12. Get cmdlet summary
#13. How much time before I can go home?
I wrote many of the script blocks so that you could use them in your own functions and scripting and tried to make them re-usable as possible. They have no error handling and assume you have all the necessary permissions. Think of them as quick and dirty PowerShell snippets. The script file is simply defines the 13 script blocks. After each one is a commented example of how you might run it.
Again, I'm not promising anything life-changing but I hope you'll have fun with them today. Download the script file 13ScriptBlocks.ps1.
2 thoughts on “Friday the 13 Script Blocks”
Comments are closed.