stephen lam

blog about nothing

PicoCMS v.2

PicoCMS version 2

As of this post, PicoCMS released version 2.0 beta 2. As my previous post might have mentioned, but this website is running on PicoCMS. While setting this website up, I fell in love with using this, and it's simplicity. I think once version 2 is out of beta, I might update this website to use version 2.0. But reading the change log, it seems like I might need to do quite a bit of work to convert this site over to version 2.0. With another child coming and just a general lack of spare time, I'm not sure it's something I have time for.

I guess I'll take the wait and see approach, and perhaps there are some features that are must have that I want to try out.

Visual Studio Code - PowerShell editor of choice

In the past year I had migrated from Quest ActiveRoles Management Shell cmdlets to the standard one that comes with the Remote Server Adminstration Tools. With that change, I also started to look for alternatives to PowerGUI. The main reason I started to look for alternatives is because the software was out of date, and it was missing features from other ISE/IDE.

The first thing I tried was Sublime Text. It's a great editor, and I used it to create this website. But for PowerShell, it was missing features that I love in PowerGUI. Features like Intellisense and the ability to run code on the fly within the script, basically I want an ISE and not just an editor. In May, I started to look for alternative to Sublime Text and noticed that the Visual Studio Code (VSCode) with PowerShell language support had mature enough that it was a viable solution to my needs.

I start playing around with VSCode and really get a feel of it. At first, VSCode was ok, it fitted my needs better than Sublime Text, but it didn't feel as good as PowerGUI. The major pain point is the terminal, it's just plain weird. There's weird rendering issues, where input would overlap text and all sorts of weirdness that I never see in PowerGUI. Also you can't paste blocks of code directly into the terminal, as you can with PowerGUI or in a PowerShell command prompt, and to this day, you still can't do that. Instead of going into minute details of the pros and cons of VSCode for PowerShell, I'll do a list instead.



Splatting in PowerShell

While changing all scripts that touches Active Directory from using Quest ActiveDirectory module to the built in ActiveDirectory module I discovered Splatting. Apparently this has been around since PowerShell v2, so perhaps other PowerShell users aren't aware that this exists, like me.

This technique allows a code like this:

Get-ADUser -Filter * -Properties * | Select-Object 'Name',
 @{Name = "Username"; Expression = {$_.SamAccountName}}, @{Name = "Title"; Expression = {$_.Title}}, @{Name = "Title (FR)"; Expression = {$_.ExtensionAttribute1}}, @{Name = "Department "; Expression = {$_.Department}}, @{Name = "Department (FR)"; Expression = {$_.ExtensionAttribute2}}, @{Name = "Accreditation"; Expression = {$_.ExtensionAttribute3}}

to something that's more readable like this:

$Properties = @{
   Property = 'Name',
   @{Name = "Username"; Expression = {$_.SamAccountName}},
   @{Name = "Title"; Expression = {$_.Title}},
   @{Name = "Title (FR)"; Expression = {$_.ExtensionAttribute1}},
   @{Name = "Department "; Expression = {$_.Department}},
   @{Name = "Department (FR)"; Expression = {$_.ExtensionAttribute2}},
   @{Name = "Accreditation"; Expression = {$_.ExtensionAttribute3}}
Get-ADUser -Filter * -Properties * | Select-Object @Properties

It makes it super easy to take a look at what I'm trying to do here, and the best part is that it makes it so easy to edit. Give it a try in your own script.

PowerShell Account Expiration

While writing an user administration script to add users, I noticed that all my new created temporary workers had the wrong Account Expiration date on them, they were all off by a day.

The code in question is the following:

Set-ADUser -Identity $samName -AccountExpirationDate (Get-Date $endDate) -Credential $creds

So what happens is when we pass the $endDate (ie: March 09, 2017) variable it actually passes March 09, 2017 12:00:00 AM. So when we look at the user object with in Active Directory Users and Computers, you'll see that the account expires end of day March 8th. Which makes total sense, but we actually want the account to expire end of day March 9th. So the easiest fix is to do the do this:

Set-ADUser -Identity $samName -AccountExpirationDate ((Get-Date $endDate).AddDays(1)) -Credential $creds

Such a small little thing that I would never have noticed, but when you think about it. It makes a lot of sense why it did what it did, and what we have to do to compensate for it.

Funny thingy about Format-Wide for PowerShell

I had created the following function a while back to display an Array of String in a nice way through columns, and not a long list as the default for Write-Host.

function format-displaylist{
  Display an array larger than certain members into nice column(s) for
  easy readability
   HelpMessage="Use the name properly of a multi-dimension array")]
  {($_ -le 10)}{$column = 1}
  {($_ -gt 10) -and ($_ -le 15)}{$column = 2}
  default{$column = 3}
 $tempArr = @()
 $tempInx = 1
  $false {
   foreach($item in $arrayList){
    $tempArr += "$tempInx) $item"
    $tempInx = $tempInx + 1
  $true {
   foreach($item in $arrayList){
    $tempArr += "$tempInx) " + $item.Name
    $tempInx = $tempInx + 1
 Write-Host ($tempArr | Format-Wide {$_} -Column $column -Force | Out-String)

So the issue with that function is that it adds 3 carriage return to the last line, and made the whole thing look ridiculously ugly looking. It bother me, but I didn't have time or willingness to take a look into why it would do that. That is, until today. While writing a new script, I was constantly calling this function and the displaying of the 3 carriage return was driving me mad. I decide to delve in and see what's causing it, and what I can do to resolve it.

After trying numerous other things, I noticed was Out-String, and I thought to myself why I was doing that. I'm not planning to work with the string, I just want to display the string itself. So I replaced:

Write-Host ($tempArr | Format-Wide {$_} -Column $column -Force | Out-String)


$tempArr | Format-Wide {$_} -Column $column -Force | Out-Host

and it gave me an output that I'm happy with. There's still return carriage (2), but it's something I can live with.

1 of 3