I have the following code in my project that installs linux sl on Windows using PowerShell. Is there anything you'd improve about this?

param([String]$Profile, [Switch]$Help, [Switch]$Force)

$ErrorMessages = @()

$Payload = "
# <Code inserted by Install-Steam-Locomotive>
function Steam-Locomotive {wsl sl -e}
function Steam-Locomotive-Force {wsl sl}
# </>

$HelpText = "This script helps you use the tremendous `"sl`" program in Windows PowerShell.
Simply download the .ps1 file and execute it.
If the script finishes successfully, you can type Steam-Locomotive in PS to start the interruptable `"sl -e`".
Use Steam-Locomotive-Force to prevent interruption (`"sl`").
PS C:\Users\User\Downloads\> .\Install-Steam-Locomotive.ps1 -Profile $PROFILE

if ($Help) {
  Write-Output $HelpText

if (!(Get-Command -Name "wsl" -CommandType "Application" -ErrorAction SilentlyContinue)) {
  $ErrorMessages += @("You don't have WSL installed. Cannot continue.")

if (!$Profile) {
  $ErrorMessages += @("Please supply your profile location under -Profile. Cannot continue.")
  if (Get-Content $Profile -ErrorAction SilentlyContinue | Select-String "Steam-Locomotive") {
    $ErrorMessages += @("Your profile seems to already contain something called `"Steam-Locomotive`". Use -Force to continue anyways.")

if ((Get-Command -Name "Steam-Locomotive" -ErrorAction SilentlyContinue) -and !$Force) {
  $ErrorMessages += @("It seems a command named `"Steam-Locomotive`" is already installed. Use -Force to continue anyways.")

if ($ErrorMessages) {
  Write-Output $ErrorMessages

if (!(wsl command -v sl)) {
  Write-Output "First, install SL."
  wsl sudo apt install sl

if (wsl command -v sl) {
  Add-Content $Profile $Payload
  Write-Output "Done!"

if (!(wsl command -v sl)) {
  Write-Output "Failed installing wsl."

Write-Output "Success!"

EDIT: I had an error in this script. The fixed version is up on the project page.

  • \$\begingroup\$ When I read the code, I thought that wsl would be the Windows version of sl. Nice confusion. :) \$\endgroup\$ Commented Feb 20, 2020 at 21:19
  • \$\begingroup\$ Who or what is sl? Set-Location is already built-in. Is that the bloody steam locomotive that shows up when using ls by mistake in a Windows environment? I usually fix that by actually installing ls... \$\endgroup\$
    – Mast
    Commented Feb 20, 2020 at 21:27
  • 1
    \$\begingroup\$ I'd recommend you edit the title to describe/summarize the purpose of the code, as strongly suggested by the watermark in the title textbox. As it stands, any powershell question could have that title - and without "powershell" in the title, literally any on-topic post on this site could have that title. \$\endgroup\$ Commented Feb 20, 2020 at 21:28
  • \$\begingroup\$ @Mast It's the locomotive that shows up when you type sl in linux... if you have it installed. This is if you want the animation in PowerShell. \$\endgroup\$ Commented Feb 21, 2020 at 15:20

1 Answer 1

  • Use the script template for documentation
  • Use a cmdlet binding for parameter validation and help text
  • Personally, I run with $ErrorActionPreference = "Stop"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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