Random VS Code tips
Visual Studio Code is my go to code editor. If you’re still using PowerShell ISE you need to start using VS Code right now! The recent books I’ve read on PowerShell scripting recommend using this tool for PowerShell scripting.
With the PowerShell extension installed you get syntax completion. VS code also integrates with Git if you’re using source control.
It has a bit of a learning curve but it makes things so much easier. One of my favourite features is the ability to format the document and the explorer pane where you have a workspace with all the folders you need to work with. Below are two things I learned from others.
Multiple Selections
Alt+Click is used to add additional cursors so that you can edit multiple lines at the same time. So for example, say you want to add some text, orc- to the beginning of hostnames:
You could also use Ctrl+Alt+Down or Ctrl+Alt+Up that insert cursors below or above the current line.
Ctrl+D will select the word at the cursor instead of having to click and highlight to select the word.
Ctrl+Shift+L will select all occurrences of the word that you currently have selected
The link in the resources section below has animated GIFs that better illustrate what the keyboard shortcuts do.
Auto splatting
One of the benefits of splatting are that your command is easier to read especially if you have many parameters which would make the line very long. So this…
Get-CimInstance -CimSession $cimsession -ClassName "Win32_TSGatewayConnection" -namespace "root\cimv2\TerminalServices"
Becomes this, which I think is slightly easier to read and makes it tidier in a script:
$getCimInstanceSplat = @{
CimSession = $cimsession
ClassName = "Win32_TSGatewayConnection"
Namespace = "root\cimv2\TerminalServices"
}
Get-CimInstance @getCimInstanceSplat
Another benefit is that you can use the same splatting values when you need to use the same command in different places in your script.
I have also used splatting to provide additional parameters for a command if a condition is met. In the example below I only supply the credential parameter if the credential variable exists.
$params = @{
computername = $computername
}
if ($Credential) {
$Params['Credential'] = $Credential
}
$session = New-PSSession @params
Anyway, what I really wanted to say is that I found this really useful module to Automatically convert a PowerShell command to use splatting in VS code.
Below is a quick demo on how it works.
Open any folder in the Windows Subsystem for Linux
Another useful thing I learned recently is that you can launch a new instance of VS code from WSL by typing code .
in the terminal. You’ll need to install the WSL extension for VS code. The benefit is that you get all the features of VS Code. The commands, extensions and terminal run on Linux while the UI runs in Windows outside the WSL terminal.
Conclusion
There are lots of other handy features and extensions, I am still a novice when it comes to VS code but I am learning little by little. What are some of your favourite VS code features or extensions? Do you have a favourite code editor. Please write them in the comments section.