Well here are once again and another Friday the 13th. I love these days because I feel challenged to come up with 13 PowerShell related tidbits which I hope you'll find fun and maybe even a little educational.
Today's collection should work primarily on PowerShell 3.0. Most items might also work on PowerShell 2.0. A few items will definitely require PowerShell 4.0.
#requires -version 3.0
<#
Most of these will also work on v2. A few are v4 examples
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
#>
#1 Get 13 to the 13th power
[math]::Pow(13,13)
#2 Create a 13 character string with PowerShell 4.0
-join (33..123 | Get-Random -count 13).ForEach{$_ -as [CHAR]}
#for v3
-join (33..123 | Get-Random -count 13 | ForEach{$_ -as [CHAR]})
#3 Get the 13th process
Get-Process | Select -Skip 12 -first 1
#4 Get 13 odd numbers
1..50 | where { $_%2} | Select -first 13
#or in v4
(1..50).where{ $_%2} | Select -first 13
#5 Get 13!
1..13 | foreach -begin {$x=1} -Process {$x*=$_} -end {$x}
#6 Count by 13s
for ($i=13; $i -lt 100; $i+=13) {$i}
#7 Get 13 oldest temp files
dir $env:temp -file | Sort LastWriteTime | Select -first 13
#8 Get the square root of 13
[math]::Sqrt(13)
#9 Get the date and time in 13 days 13 hours 13 min and 13 seconds
(Get-Date).Add("13:13:13:13")
#10 Get the date and time that was 13 days 13 hours 13 min and 13 seconds in the past
(Get-Date).Subtract([timespan]"13:13:13:13")
#11 Open the 13th most recent post on PowerShell.org in a browser
Start-Process (Invoke-RestMethod -Uri http://powershell.org/wp/feed/)[12].Link
#12 Create 13 dummy test accounts in your domain
1..13 | foreach { net user "TEST$_" P@sswOrd123 /add /domain }
#or remove
1..13 | foreach { net user "TEST$_" /delete /domain}
#13 Get the next Friday the 13th.
$i=0
do {
$i++
$next = (Get-Date).AddDays($i)
} until ($next.DayOfWeek -eq "Friday" -AND $next.Day -eq 13)
Write-Host "The next Friday the 13th is in $i days on $($next.ToShortDateString())." -ForegroundColor Red
I'm not even going to show you any results. I'll let you try them out on your own. Enjoy and stay out of the lake today.
#Get the 13th of the Common Era:
([datetime]0).Add("12:13:13:13").AddYears(12).ToString('MM-dd-yyyy HH:mm:ss')
And since you are madly collecting 13 type things, here are all Friday the 13ths in the past:
$d=([datetime]0).AddDays(4)
while($d -lt [DateTime]::Today){$d=$d.AddDays(7);if($d.Day -eq 13){$d.ToLongDateString()}}
Thanks!
Here’s a bonus trick: Get the last 13 lines of a log file:
get-content C:\windows\WindowsUpdate.log -last 13