It was Ben Thomas that gave me the prodding to look into Visual Studio Code (VS Code) for PowerShell coding and troubleshooting. I had an error in a PowerShell step that puzzled me. It turned out to be the auto-replaced hyphens that got introduced into that PowerShell step somewhere along the lines since I keep (kept) everything in OneNote.
There are several reasons why coding in any form are difficult for me, suffice it to say it took a few days to get over the, "Oh no, yet something else to learn" initial reaction to that prodding.
With a little downtime late last week, the opportunity presented itself to at least do a cursory search and skim of info on VS Code and PowerShell.
What I saw amazed me so much so that the time to learn became a non-starter.
First, download VS Code but don't install it right away.
Next, download GIT for Windows (there's other versions).
Now, there's a bit of a Catch-22 in this process as Git looks for VS Code and VS Code looks for Git.
Install VS Code and Git
Install order to make things simple:
- Install VS Code
- Run VS Code and ignore the Git prompt
- Install Git and choose VS Code
- This is where things can get weird. If VS Code does not get started first, the Next button will not light up!
- If not, leave this window, start VS Code, ignore the prompt, and close it.
- Hit the Back button and then the Next button again and the Next button on this window should now be lit up.
- We chose Use Git from the Windows Command Prompt
- On the next window Use the OpenSSL Library
- Checkout Windows-style, commit Unix-style line endings
- Use MinTTY (the default terminal of MSYS2)
- We left the defaults for Configuring extra options
- Enable file system caching
- Enable Git Credential Manager
Once Git has been installed the next thing to do is to start VS Code and it should find Git:
Initialize the Git Repository
A few more steps and we'll be ready to code a new PowerShell script, or transfer in from whatever method we've been using prior.
- Create a new folder to store everything in
- We're using OneDrive Consumer as a location for ours to make it easily accessible
- CTRL+SHFT+E --> Open Folder --> Folder created above
- VS Code will reload when the folder has been chosen
- CTRL+SHFT+G --> Click the Initialize Repository button
- Click the Initialize Repository button with the folder we opened being the default location
- Git should be happy
Now, we're almost there!
VS Code Extension Installation
The last steps are to get the PowerShell Extension installed and tweak the setup to use it.
- CTRL+SHFT+X
- Type PowerShell in the Marketplace search
- Click the little green Install button then after the install the little blue Reload button
- Additional VS Code Extensions we install by default
- Better Comments
- Allows for colour coded # ! PowerShell Comments
- Git History
- Allows us to look at what's happening in Git
- VSCode-Icons
- Custom icons in VS Code
VS Code Quick Navigation
Once done, the following key strokes are the first few needed to get around and then there's one more step:
- Source Control: Git: CTRL+SHFT+G
- Extensions: CTRL+SHFT+X
- Folder/File Explorer: CTRL+SHFT+E
- User/Workspace Settings: CTRL+,
Create the Workspace
And finally, the last step is to:
- File
- Save Workspace As
- Navigate to the above created folder
- Name the Workspace accordingly
- Click the Save button
Then, it's Ready, Set, Code! :)
Note that the PowerShell .PS1 files should be saved in the Workspace folder and/or subfolders to work with them.
To start all new files in the PowerShell language by default add the following to User Settings
- CTRL+,
- "files.defaultLanguage": "powershell"
One of the beauties of this setup is the ability to look at various versions of the files, much like we can with SharePoint and Office files, to compare the changes made over the history of the PowerShell Code.
Another is the ability to see in glorious colour!
Thanks to Ben Thomas' challenge PowerShell is already so much easier to work with!
2018-08-15 EDIT: Oops, missed one important step.
- Open Git GUI
- Click Open Existing Repository
- Navigate to the Workspace folder with the .git hidden folder
- Open
- Set the User's name and E-mail address for both settings
- Click Save
Git should be happy to Commit after that! :)
Philip Elder
Microsoft High Availability MVP
MPECS Inc.
Co-Author: SBS 2008 Blueprint Book
www.s2d.rocks !
Our Web Site
Our Cloud Service
No comments:
Post a Comment