Getting Started with Powershell in VS Code

Ok, it is time. Time to understand scripting.

I have been using Scratch Org for a while now and it is tedious to set them up. But Iโ€™ve been scared of scripting.

  1. because most devs are mac users

  2. because most devs think you just know about bash and how to use bash

  3. because admitting you donโ€™t know about scripting is like admitting you donโ€™t know how to breathe to some devs

  4. because powershell seems like itโ€™s own coding language

Now, at least I have touched unix in a previous life and know about some unix commands and have used vi back in the dim dark distant past, so at least I would know how to quit vim these days, or at least know that there is a trick to quitting it and why that trick exists.

End Result:

  • I want to have a few aliases to shortcut DX commands

  • I want to have a script that I can create a scratch org with

Thatโ€™s about it.

Trailhead

This module might be helpful https://trailhead.salesforce.com/content/learn/modules/cli-basics

But you DO NOT NEED NODE OR NPM! (Yet). So ignore the Mac specific bits in the third module.

Getting Started

Well, you need to start at the beginingโ€ฆ and gee is that hard. First of all just how many versions of Powershell are there? And does it need to be stand-alone or can it run in VS Code?

So keeping thing simple I started using the Integrated Powershell Terminal in VS Code - I donโ€™t want a separate terminal.

When googling, you are not writing Powershell Scripts in VS Code, so itโ€™s quite confusing.

The Integrated terminal in VS Code defaults to Powershell in Windows 10. See integrated terminal docs.

Use the Command Terminal: Select Default Shell to set Powershell as the default shell.

When you open Terminal PS then the folder name should be shown as the prompt

PS C:\Users\JodieM>

Set the Profile

I started with Peter Chittumโ€™s Apply the Salesforce CLI to Everyday Problems. Thankfully Peter is talking about Powershell and Bash these days. I went to the Github repo and all of a sudden was stuck. What is the ISE? Wh do I need to know about that? What is a profile? And everything past about half way in that video is a bit over my head - when he starts talking about โ€œtailโ€, then I know heโ€™s not talking to me. Itโ€™s like devs who just say โ€œtouchโ€ the file. You know you are talking to a Mac dev and you know to just ignore and walk away and try to find someone that can speak your language.

So I think what heโ€™s doing is trying to get you to have a default set of commands that open up each time you open VS Code. This is stored in a profile it seems. I could not work out Peterโ€™s instructions though.

I found a simple set of instructions that were basically the same as Peterโ€™s instructionsโ€ฆ the trick is to start in VS Code and donโ€™t leave VS Code. And donโ€™t install the Powershell VS Code Extension.

I found this article to be helpful.

Test-Path $profile

This returns false

New-Item -path $profile -type file โ€“force

this returns the folder where the profile is stored. I opened the folder in my file explorer then opened the profile file in vscode.

The file isย Microsoft.PowerShell_profile.ps1ย and is created inย C:\Users\<username>\Documents\PowerShell\

You may also need to do the following. โ€œIf the Execution Policy is set to RemoteSigned, you can run only foreign scripts from Trusted Publisher, but you can also run your own scripts (that are not signed).โ€ From this article.

Paste the contents of Peter Chittumโ€™s sample profile into that file everyday-sfcli/utils/powershell-profile/Set-RecommendedProfileItems.ps1 at master ยท pchittum/everyday-sfcli. But remove the cd~ at the top as that changes the directory out of your current working SFDX directory and you donโ€™t want that.

Save, then Reload VS Code.

Open the terminal and type:

A list of your scratch orgs appear! Magic!

We now have aliases!

still works too!

Extending Functions

Now we can see in Peterโ€™s example that functions can be a bit more interactive.

so now we can see we can add parameters to functions. The $u is saying whatever is entered directly after dxopen then stick it after the command sfdx force:org:open -u

So I want to add another command like that to set default org.

sfdx force:config:set defaultusername=

Reload VS Code

Type

The default org is now set.

Once the default org is set, you can use the window icon in the status bar to open that org.

Itโ€™s pretty easy to just remember ctrl+shift+p start typing sfdx then arrow to the command you want to use, but having different options is good.

Thatโ€™s enough for now. At least we have some simple aliases.