Hello, PowerShell!

Last week, while I was at Abstractions, I heard that PowerShell for Linux was released. Today, I tried it out!

My desktop machine at home is currently running Xubuntu 16.04.1, which is one of the platforms already packaged up. I downloaded the .deb, checked its sum

$ sha256sum Downloads/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb 
5d56a0419c23ce879dd4ddaca009f03e888355fccc9eecf882b64d63da5f38e3 Downloads/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb

and followed their instructions. I already had the two dependencies, so

$ sudo apt install libunwind8 libicu55

had no effect. Installing their deb

$ sudo dpkg -i ~/Downloads/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb

gave me a powershell executable.

$ which powershell
/usr/bin/powershell

Writing a quick hello world in PowerShell with that as the shebang line

#!/usr/bin/powershell

$name = $args[0]
if (!$name) {
    $name = "World"
}

write-host "Hello, $name!"

worked great!

$ ./hello.ps1
Hello, World!

$ ./hello.ps1  foo
Hello, foo!

Okay, how about those regexes with multiple named captures I talked about a while back? If we write this in multicapture.ps1

#!/usr/bin/powershell

$string = 'foo bar baz'
$pat = [regex] "(?:(?<word>\w+)\W*)+"
$m = $pat.match($string)
$m.groups["word"].captures | %{$_.value}

then lo and behold we get

$ ./multicapture.ps1 
foo
bar
baz

Now that I don’t have to boot Windows to do it, I might play with PowerShell a lot more! Thanks, Microsoft!

Advertisements
Hello, PowerShell!