Using Visual Studio Code as Your Default PowerShell Editor by Tyler Leonhardt

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right I think we could probably get here so today I'm gonna be talking about using Visual Studio code as your default powershell editor now hi everyone hi my name is Tyler that's me if you couldn't tell we look a lot alike I map I'm a software engineer on the PowerShell team at Microsoft this is actually a picture from the Space Needle for those of you who have been up there they've remodeled it recently I'd highly recommend checking it out that's my handle on Twitter right here if you follow me or if you don't follow me yet check that out it's also my handle on twitch where I do some live streaming namely live coding different things in the PowerShell world there's actually a pretty big community of live streamers in the PowerShell community these days so I highly recommend looking into that PowerShell live is the handle both Twitter handle and twitch handle for a bot that like hosts all of the PowerShell community and if you have more questions about that feel free to talk about that with me afterwards but with that said let's get started today you like that it's a good it even goes backwards when you go back it's pretty good one more time after you are like that's amazing the other half is like why today we're gonna be answering a few questions first thing is what is Visual Studio code the next is how vs code can help us write better PowerShell scripts and modules then we'll talk about some ISC compatibility stuff and making the PS code very much so like the IC experience and a little touching on what's to come I do want to start out by saying this is kind of a beginner focused talk so if you're already like a heavy user of the partial extension this is probably not the talk for you but you know I want to give you a chance to like go to some of the other talks that you were you know if you were on the fence with this talk and some other I would not be afraid or sad if you walked out right now but I just want to give you that as a heads up all right sweet so I want to introduce you to my friend here his name is Joey and he's pretty sad and why is he sad because he keeps hearing about vyas code but he doesn't know where to get started or he keeps hearing about vias code but he really doesn't understand the hype so he said about that and this do you people know you know what this is it's Joey Wow that looks awful on this high resolution yeesh anyway so Visual Studio code Visual Studio code is product by Microsoft it's a free open source lightweight text editor slash IDE it provides it's got this nice extensibility model you can install extensions with it and it's overall just a very nice text editor for whatever you're doing I want to be super super clear and say vs code is not Visual Studio they are two completely different products and and are built completely differently over on the left is Visual Studio look for the purple logo and then over here is Visual Studio code the stable is the blue one and the insiders is the green one the insiders updates every day the blue one is is every month or so so and in case you were wondering what the new logos are gonna look like that's what they're gonna look like so be on the lookout for those I think they look pretty nice in my opinion anyway so B's code and PowerShell are actually great together and we do that thanks to the PowerShell extension for vyas code so we mentioned that it's extensible and so we have a couple PowerShell extensions one PowerShell one PowerShell preview and if it wasn't obvious the preview is well a preview and it's what's to come in PowerShell extension so that one definitely has more bugs in stuff that we're trying to make stable but I will ask you all please try the preview because that's you know the latest and greatest that we're trying to ship into the main product so those are the two ones if you go to the extension pane envious code which is this little square one and then search for PowerShell you should see them and you also see atom Driscoll's partial Pro Tools which is just testament that like people are starting to adopt vs code for for other extensions as well powershell related in in vs code so hoping to see more powershell related extensions for vs code all right now we're gonna blow through all the the features really quick i didn't want to go back and forth between slides and and an editor so i just recorded tiny little clips of some of the features but there will be some actual demos that we'll do later all right number one intellisense and tell a sense for those of you that don't know is is this it's this you get all the completions for what for what you're typing for in addition to that you also get this stuff on the right which is like the the syntax of that command if if a command has that available to it it can definitely show you that kind of stuff along with other hints so that's intellisense there's also cool things like snippets I believe snippets are in in the ISE as well but envious code this is what they look like so they'll show up in the intellisense so here's an example of a commandlets and if i run that one it's gonna give me a verb noun function here and then i'll go into the parameter block and i'll type parameter and use a parameter snippet as well and you see there's a bunch of different options here okay parameter snippet and boom there i have a parameter and as I'm going through this I'm hitting tab which will take me to the next section in that snippet which is very handy for for writing really really quick stuff so that was maybe like five characters that I actually typed with a bunch of tabs and and I got this whole function so it's super helpful in in writing things quickly and kind of scaffolding out your module another thing that's big in terms of helping out is the references code lens feature which looks a little which looks a little like this so for those of you who have tried Visual Studio in the past you might be familiar with code lens it's this little extra text that lives on top of the function there that's a zero references and once I hit this video it will actually change the one because get cat fact was then called in the script and so let me just pause this really quick there you go and so now we have that single reference up at the top and if I click on that single reference thing it's gonna pop up this this dialogue here and on the right it's gonna list all the different references that are available and on the left it'll give you a little preview of where in the text that is so it's it's really helpful when you have large projects with many different scripts because also it works across across files so over here I have a module and a regular ps1 and I could see the reference in my ps1 and I can easily go back and forth between those the thing that I did before was I hit f12 with this test module command highlighted and it jumped me over to the to the module so the point here is it's very easy to kind of navigate around your projects for those of you that are all familiar with the ISC there is this concept of executing a selection with f8 and that's also supported in the extension basically in this demo I hit f8 on get cat fact here and of course that function isn't available in my run space below so I'll select all of it and then run it again and then now that that function is loaded in my session and sorry that's a bit tough to read but I can hit f8 again on get cat fact and then it we see that Thomas does in fact love cats hi Thomas so so that is a mechanism for having code in your editor pane and getting it down below into the console there's also a way to do kind of the reverse of that and in the ICU was also called PS edit and the idea here is that you write in your integrated console PS edit and then a file path and I'll actually go and open it in your editor and that's what this looks like so down below you'll see I'm just gonna like cat my my profile and naturally there's just a bunch of text there and ideally I'd want to open it in my in my editor pane so you can either type PS edit which is an alias for open editor file and so I'm gonna do open editor fire file dollar profile and then that's going to open it right in my in my editor super handy for you know your navigating the file system in the console you want to quickly open up a file in NBS code you can easily do that in addition to open editor file there's also a new editor file which sounds pretty self-explanatory what I'm gonna do here is whoops ah my goodness sorry I'm used to PowerShell not PowerPoint I'm sure other people have said this same thing this talk anyway so I'm running it process which we all know gets all the processes as the name implies but then I'm gonna tape that output of get process and I'm going to pipe it into new editor file and it's actually going to open an untitled file in my editor with the contents of get process so it's really helpful when you want to just preview the output of something and it opens a untitled file so this is not a file that lives on your file system it's all like in memory with envious code and new editor file also has like a path parameter which will let you specify a path if you did want to do that so and I also want to point out that all of those those two commands open and new editor file work in remote sessions as well so you could go and do entropy accession down below here and then do PS edit whatever or new editor file whatever and that's going to open it right in your editor feeling as if it was a local file but it's actually a file that lives on the remote machine so and that's a fun feature I think I actually demoed that last year for those of you that we're here alright so now I'm gonna get into into debugging and figurative video doesn't really do this justice so I'll do an actual demo for this one so switch over to vs code here and I've got this pretty pretty simple script you zoom in a little bits here there we go you see that said all right in the back cool cool so I've got the pretty pretty simple script here it takes in parameter and we're invoking this rest method for the cat facts ninja slash fact API which as the name implies gets you a random cat fact I learned about this in Thomas's talk who we know loves cats I learned about it in his talk earlier this week so thanks for that so anyway so what we can do is we can debug this by just hitting oh sorry I was doing this demo earlier and I need to go back to my before talk State ok so I've got my script open here and all I'm gonna do is hit f5 and what f5 is gonna do is it's going to run the script with the debugger and you'll see that I've got this like highlighted line here and that's because it stopped at a breakpoint there and before I had placed a breakpoint this little red dot on line 17 and so now while I'm running it with the debugger it's now stopped there and there are a few things that I can do now that it's stopped there for starters I can like hover over this URL and see that it is in fact catfac dot ninja slash fact so I could see what that looks like and I can also see it over here on the left side and the left side see that it shows up here in the variables section and there's there's a lot more to the debugger I'm not going to get too in-depth because this is more beginner focused but you get kind of an idea of the experience that we have here I can also evaluate stuff in the bottom here so you know I can do dollar URL and see that it's set to two what we expected this be set to or even so I could change it to be C API dot github.com slash octocat and then we'll see that over here if we if we step over that we'll see the URL has changed here and our response that we get back is not the response from cat facts but actually a hello I guess it's gone inside or build a Visual Studio code I guess let me try running that again oh I forgot to change the URL sorry about this and continue and we see we get the the octocat here from github with a little quote there so you can kind of see that like this kind of experience allows you to go through your script change things that need to be changed or or test out different things and ideally write more robust scripts that way one of the features within Visual Studio code that's a little bit more on the advanced side of the debugging stuff is they have different debug configurations and and that's what this stuff is at the top left here and if I hit the drop down here I can add a configuration and what this does is if I go back to my file explorer here is that it adds this launch JSON under a dot vs code folder in my directory and it sometimes comes pre-populated with a bunch of stuff but in the event that it doesn't if I start typing here PowerShell I'll see all the different debug configurations that we ship with in the extension by default so for example by default when you when you hit f5 it does the launch current file thing but let's say we wanted to use this one launch current file with args which will prompt to see if you want to pass in any arguments to your script so going back to our test script here I'll hit f5 to run it and I actually get this little dialog that says pass in any arguments that you want to your script so for this fact link I'll put like two hundred and we'll actually see that for the URL it's gone through that that if statement above and has added at the very end the max length equals 200 there and then I can continue and we'll get our fact response now what I wanted to do is just call just spit out the string of the fact so I can add that hit f5 continue and there we go I got my got my cat fact so that's just like a little taste of the like debugging experience it certainly must there's a lot more to the debugging experience and there are docks within like vs code on on all the different kinds of things that you can do with the debugger but this is like kind of the kind of a taste of it the ideal situation is that you can install the extension and then immediately like hit f5 on your script and debugging will operate in a in a predictable way so that's that's what we wanted to achieve we didn't want you to have to mess with all these debug configurations from the get-go so hoping that it's pretty simple to get started with that alright and and like I said there were a few other debug configurations that I don't think we need to get into right now but but yeah check those out because some of them are pretty good all right see so as we see debugging is a great way for writing scripts and and making your scripts much more resilient and the thing is is that a lot of people actually say this when they switch to using vias code they'll say after switching to BS code I've noticed a drastic increase in the quality of my scripts and modules and these are real people I promise and one of the the biggest features with envious code that that really helps make this a reality is the integration with script analyzer now for those of you that were here yesterday Chris Berg Meister and I see him in the room hi Chris he he did an awesome talk on PS script analyzer and some of the new stuff that's been going on with that and so I'm gonna show hopefully quick demo of that and if you were more interested in that part of the talk definitely see Chris's talk when it goes up on on YouTube after the conference oh yeah demo there we go so back in vs code I've got a script here called PSA test script now the script does doesn't really do anything useful but the reality is it does use some new constructs and commandlets that don't exist in every single version of PowerShell in addition to that it's got some like extra spaces here which is weird and I'll add like a a GCI here so it uses an alias so we notice here that I've already got this like green squiggly under GCI and if I hover over it I see GCI is an alias of get child item blah blah blah blah basically saying that you should probably write get child item because you should think about the person that you're gonna hand your script off to next right in theory you wouldn't want a script full of aliases that that people don't necessarily know and so it's best to use the action command this is just one of the very many rules the script analyzer supplies to help make your scripts much more readable and consistent one of the things that that I believe Chris demoed in his talk is some of the new work on adding this compatibility rules in script analyzer which is the the premise of I've written this script on my surface here win 10 PowerShell core 6 - but I want I need to make sure that this script also runs in Windows PowerShell v3 or something like that so if you watch us talk to might go into the details of this where are these numbers coming from weird feature yeah there we go you'll see something like this this is a script analyzer settings file and it's you don't we don't have to get into the nitty-gritty here but I'm basically saying that for the compatible SiC syntax I want to target these versions of PowerShell 3 5 1 and 6 2 and then for the commands I want to target these compatibility packs which is like this is I believe 6 1 on on core on on Windows 10 this is 5 1 on Windows 10 and then this is 3 on the windows server or something like that anyway 3 so but I need to tell the vaguest code extension to use this settings file so to do that I head over to the settings and if I search for script analysis here I have an option to put in my settings file path so I'm just gonna put PS script and settings dot PSD one like that and what I've what I've done here is that for vs code settings there's a user space and then a workspace space and at the workspace level it actually drops a settings file in that dot vyas code folder that it made before so let's say you have a module and you want to enable certain settings just for that module you can do that with this mm-hmm and so if I go to some other folder and that settings file won't be there so those settings won't get applied but for this case I want to only set it for this misc folder all right so I'll go over here and just like that I I had already like warm this up a little bit but in typically the first time that it goes through the compatibility rules it takes a little bit of time but now you see I've got a bunch more extra squigglies that I need to go through one here says the constructor syntax of colon colon new was introduced in or doesn't support three and four and get file hash is not available by default in partial version three on Windows Server 2012 data center and and stuff like and even at the parameter level two so there's just all of this information now that I can guarantee that the script that I'm writing here on my win 10 with power stroke or 6-2 can work on these other platforms so that's kind of like one of the the benefits of using core is that you can like you could do it in core and use core and still target other other platforms if you need to so that is a script analyzer and by and by using this it does help make your scripts more consistent and and and hopefully much easier to pass off to some other person so yeah yeah solved as in just for this individual line I don't know sorry I'll have to find out and let you know about that come talk to me afterwards so let us go back to slides here so another what am i doing okay so that's the script analyzer integration it also handles formatting as well so it will like change your script to make sure the whitespace is consistent and stuff like and stuff along those lines so a lot of a lot of power comes from the script analyzer integration there's also another integration with pester and pester tests and what this pester test integration looks like aside from just some some snippets that exist is that it'll actually give you the little code lenses above your describe box and so what this allows you to do is you'll click on those and it will run them in the console below right but the powerful one is definitely at the debug tests or you could place breakpoints hit debug tasks and it will actually like stop at that breakpoint within your test so that you can debug you know why your test is failing or something along those lines so that's to help make the make writing pester test much much easier and and friendlier in my opinion that way you don't have to like go down here and like manually invoke it yourself all right so now the next section I want to get into is in terms of ISC compatibility and I think I have to start with the ISC theme already been using it the whole time just because it's it looks better on the big screen but this is how you would enable it it's it's quite straightforward you open up the command palette which is control shift P or command shift P if you're on a Mac and then you just search for a theme find the color theme one and then just type PowerShell and the boom you've got the PowerShell ISE look so if that's it one of the things that you were like very worried about is not having the look and feel about of the ISE there you go you've got it and and although there is an icy theme there's also like thousands of themes out there if you want something different so yeah go crazy with the with the themes I know I do the other thing I want to show off is tab completion now we saw that there's intellisense but a lot of people are used to typing something and then hitting tab and tabbing through all the different options right this is what that looks like so first I'm gonna enable the setting so I'll go into the settings UI here search for tab completion make sure that's on go back to my script and then type get - and then from there I'll just hit tab and I'll continue to hit tab which will cycle through all the things that I can possibly complete - so I know there used to be an issue opening in the vias code powershell repo because people wanted this experience and then vs code just one day said alright here you go so thanks B is code yeah so if you're if you're looking for that experience that's also there which is as far as I know consistent with the ISC also on IFC compatibility you don't focus console on execute so video for this is I've got get verb and I hit f8 and what happens is my cursor ends up getting it ends up down here me in the console right but that's not I don't think that's what the is he does today what actually happens is when you hit f8 the cursor actually stays put and so the setting to do that the presenter view is definitely different than what it was before bless you the setting for this is gonna show up here in a second this one so search for focus console and then switch that one off and then that will give you that that experience of hitting f8 and the cursor not jumping down to the console thank you thank you anyway the reason why this is an able or the reason why it is the way it is by default is for accessibility purposes in the vs code extension so it's not something we can change the default of but if you you know if you want that it's there yeah yes yes there is there is a key binding for that but I don't know all of the key bindings off the top of my head sorry I know I should just get off the stage now let's I see a couple hands let's just save them for the very ends because I want to make sure I get through everything and then I'm more than happy to talk afterwards as well all right so enabling it as the default language if you do a bunch of PowerShell and you really only do power so you can change PowerShell to be the default language of new files that get created and if you search for default language and type in PowerShell any new files that you open will be PowerShell files and we can verify that by opening a new file and then we'll see the console jump open and then and then intellisense will also show up as well voila so that's been a few a few different things in terms of like getting the ISE experience in vs code there's a doc that I've written about this AKMs slash ISE compat it's not complete in any sense of the word but it's a good start and it's a great way to point people to to it who are you know dreading the switchover or whatever the case may be definitely like and this this doc pull requests are always welcome so if you have some quick tip on like making that that transition easier absolutely submit it because you know we want it to be simple right alright so there is one other section I want to cover really quickly because it's it's important and it's and it's troubleshooting so I can tell you I will be completely transparent with all of you and say the power shal extension is not a perfect piece of software and again nothing is a purpose piece of software but in case you do run into some troubles here are a few troubleshooting things to to help first is to restart the session now I've heard people they like close out of the vs code instance and then reopen it again when something goes wrong and the reality is you don't actually need to do all of that you can actually open up the command pallet type restart and you'll see PowerShell restart current session and that's not gonna restart vs code it's just gonna restart the powershell extension so you don't have to go through the hassle of of closing and reopening your your files so that's one way to do it you can also click the little version number I don't know if you could see that but this guy right here if you click that that's gonna open up the same dialog as well but before you restart your session you should absolutely upload a bug report it's actually pretty simple to do it's another command palette action open that up and just type bug and there's actually an upload bug report to github and what its gonna do is actually going to open up your browser for you and fill in a bunch of details on the vyas code version powershell information and all that stuff that we need to help to diagnose what the problem was so that's very important step two to filing issues and alongside of that you have to upload the logs because the logs are absolutely crucial for us knowing what's wrong and to do that again we're back into the command pallets and open up the logs by just typing logs and this will open up another vs code instance with these log files zip him up and then paste them right into the into the issue that was opened in the previous video yeah so that is the absolute best way to give us the the information that we need to fix any issue that you might be having all right now quick look at what's to come I've got like 10 minutes left number one is right progress now for those of you that might have realized right progress doesn't work in the current powershell extension it doesn't not work it just doesn't show up so well that doesn't really work huh anyway so in the preview extension today it does work and here's the the living proof [Music] yeah thank you thank you that one person yeah so this was actually one of the like highly requested things and and we did it in the in the previous section so the number one highest thing on the list was actually PS read line which is in the preview extension today that is the the flagship feature is previous read line support so if I go back to my BS code instance which is running the powerful preview you already see I've got the syntax highlighting there right so I can do like I don't know open up curly brace and then start typing multi-line support very nice I've got what else can I do I do I'll do like a right host here and do control R to back search back search for get process that works fine it's the whole it's actually just PS read line but in the console so and there are a few things that are that are different between the integrated console and a regular power so session which has made this pretty extensive challenge to get working so there are definitely some some issues which is why it's in preview and not in stable so please try it out let us know what is isn't working and and yes so that is PS read line support exciting nice has anybody learned something yet okay good good good I'm glad I was definitely afraid no one was gonna raise their hand there all right and so the UH the only thing that I want to show off in the what's to come section is I'm calling code lens for all and the subtitle for this is what happens when I dive into a rabbit hole on the weekend and the sub subtitle is when I should have been working on these slides but anyway so I'll give you a demo of what that looks like so this weekend I had found something about my that the guy who used to work in the extension David Wilson for those of you who know him I found a feature that he had added to the extension that I didn't know existed and there's a lot of those but one of the things that he added was this ability to make your own custom custom code lenses so we saw the pester one and we saw the references one so the one I wanted to build was for for those of you that use invoke build or sake for like build automation and let me let me disable the script analyzer stuff so it's not complaining all right so have any of you heard of invoke build or sake before Brandon did a talk on sake this conference actually Brandon Olin anyway so the gist is that you have these different tasks that you set up it's a very like DSL II way of like build automation so I have this task called build which just does my my awesome build script here which just write hosts and then a deploy task and then I have the syntax here build comma which means first run the build task and then run my script block which is my awesome deploy script as well so you could see how it's like it's very like DSL you like but there's these different tasks that we want to run and and typically what you would do is you would type invoke build and then you would say like build or deploy and it would resolve all the different the dependencies of the different tasks correctly and then run them and it looks like invoke build doesn't handle rocket ships correctly what a shame anyway so so one thing that I thought was well instead of writing this why don't we just like show a little code lens thing here right and the the important thing that I have here is I've installed this module that I that I made called I'm calling it build runner vias code tools and the important thing here is that it's it's a module that exists on a partial gallery it's completely separate to the extension so there's no there's no like hard like dependency on the code being there and if I so I've installed the module and it exposes a commandment similar to you to what posh kit does for those of you that use that called add build run err profile script and what this does is if I take a look at what my profile looks like now I see that it's just added this import module down at the bottom here okay it's then when I restart my session which we all know how to do now I'll actually get little code ones that show up in BS code and this module only imports in BS code it doesn't import in a normal PowerShell console and so then with these I have the ability to just run them and even debug them as well so like totally like cool first class II experience with with these build tools right from the comfort of obvious code and I personally my favorite part about this is that it doesn't actually it's not actually part of the BS code extension it's totally separate so if you think of any other like DSL II type things like this that could use code lens code lens actions like this those are absolutely 100% possible with whatever tool that you're using so so that's another thing that is on its way I that PR is still out for this so it's not quite in the latest version but that'll be in the preview extension next release for sure so that is a cool thing all right lastly but not lastly to all of you blah blah blah I already said this pull requests are always welcome it's open source the vs code extension is open source so if you know any of these languages which I hope you know at least one of these languages this one you can contribute to the to the power so extension and this one is Doc's in case you're wondering that's the little Doc's that Microsoft com logo mascot thing but but yeah so if if this kind of stuff interests you if you are like me and you like the idea of working on developer tooling let me know and and I am absolutely available to help like guide people into contributing I'm all for that so yeah that's that's pretty much it I also have the big link slide which typically gets people to take out their phones yep yeah I should I smile okay good good good but but that's it that's that's all I've got I hope that this was this was useful and and shows how you can make PS code your default editor so thank you all [Music] you
Info
Channel: PowerShell.org
Views: 15,413
Rating: 4.8989897 out of 5
Keywords: powershell, windows powershell, techsession, powershell summit
Id: bGn45vIeAMM
Channel Id: undefined
Length: 43min 35sec (2615 seconds)
Published: Sat May 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.