Basic To Boss: Customizing Your PowerShell Prompt by Thomas Rayner

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome is anybody in here expecting to see content related to PS it's okay put up your hand you're in the wrong room there was a scheduling change and now instead in this room I'm going to talk to you about a fascinating concept of making awesome PowerShell problems my name is Thomas Turner I worked at Microsoft not an evangelist or on the product team I'm just a guy who uses the stuff who happens to have a blue badge and before you ask yes this is seriously a talk on things that I like to do and things that you can do to make your power shell prompt more interesting and useful and fun honestly why am i up here the normal prompt is boring right it looks like the PS and it's the working directory are in and lame right like come on we can do better but realistically you're always looking at it when you're in the shell right like every time you hit Enter and you'd like don't accidentally kill your entire process you see the prompt it comes up most of you probably like don't think about it even it's just there and whatever it is what it is and yeah I know I can change it but why would I do that but because you spent so much time looking at it or you could but you spend so much time seeing it at least why not put it to work why not actually use it for what it's meant for which is providing you useful information that you see every time something actually happens on your console and so there are a lot of ways to perform this activity of making your prompt more useful and I'm going to show you what I do and it will hopefully give you different ideas on what you could do or you could just pill for mine I'll make it available and then we can just have the same and that's fine by me too what could you include like instead of just having the present working directory here's just a brief list of completely reasonable that you could put it in your prompt so that you can see it every time and like on the left hand side there's things that are a little bit more predictable and reasonable like the history idea of the line that you just ran I'll show you why that's helpful the last exit code whether the thing actually worked or you don't know unless you just see the error information about get like there's lots of modules like posh get out there that help you with things like that and then on the right side it starts out kind of serious like the name of the drive that you're in that's helpful and then we end up with stuff that's a little bit more out there like your Wi-Fi status you can pull that and stick it in there if that was a valuable place for you to see it or ASCII art of cats or whatever you like to make ASCII art out of because it's fun why not look I'm serious but whatever you want in there the question is would you need a bigger monitor the answer is I don't know maybe it depends how much to ASCII art you put in there Jeff Hicks has a cool blog post on some of the crazy stuff that he's got in his you should go check out too and you know what you can do cool stuff for its own sake - like I last lad was like pieces of information that you can add but the default prompt is just white text on a blue background which again boring you've all seen it like it's useful it's functional but why don't we use some more interesting fonts and kind of have some cool characters in there with utility that you'll see colors are another thing that are fun to play with which again sounds kind of silly but this is a lighter hearted session this was supposed to go on Thursday sounds and animations please don't if it's skimpy - thank you use every day but maybe it's your April Fool's I just gave it to a co-worker prompt you know you can you can get creative here because the things gonna run every time they hit Enter so the key to unlocking all this don't read the stuff on the right it's just a screenshot of one of the docs pages on Microsoft the link is right there for reading more or just search Microsoft docs ansi escape sequences what these are if you're not familiar there are special sequences of characters that you can type into the console to make cool stuff happen cool stuff like okay now read a little bit adding an underlying changing a color changing a position of a character and where it's being placed ansi escape sequences are basically using functions of the console to make a more attractive or visually interesting thing happened and so these are the key to doing a lot of different colors it's better than just going like you could you just go right in host - foreground color or something or you can use escape sequences which don't have a dependency on the right host so if you're running somewhere that doesn't have right host or it's gonna get weird don't have to worry these are just creating special strings that tell your console to behave in a way that isn't just put text on the screen there's instructions embedded within that string WTF weird drilling funds I'm not gonna show you necessarily but this github link has a whole bunch of what are called power line fonts power line has come to mean a whole bunch of different things related to making your prop look cool this would mmm excuse me it was initially kind of more of like a linux thing people were doing more in like their bash shell power line fonts are a super set of just like regular fonts that have like letters like a and B but these also have things like get symbols in arrows that are more useful in a prompt function than the ones that you would get without a font like this and there's a bunch of different kinds and so on this repo there's a whole schwack of them and there's a script for installing them and if you trust the script you can run it and install all those fonts randomly on your computer or you can just pick and choose the ones that are actually useful - so where am I going with this and I apologize if it's a little tough to see but this is my daily driver and we're gonna break it down this is the prompt that I use like on my regular computer it's on this computer it's I'm a work computer it doesn't follow me around to servers because it honestly it's not that important for me to have there because mostly when I'm trying to figure stuff out I'm on my computer but kind of moving from left to right in the top left of God a history ID it's telling me I know this picture is small I'll show you a bigger version I've got the idea of the line that is about to run and I'll show you why that's helpful I've got the nested prompt level I'll show you to you that I've got the drive that I'm in which is super helpful if you're using something like ships or you're using anything that has a hierarchy or a mounted drive on your system the name of the actual folder I'm in not the full path but just the folder and then I've got some get stuff and a bunch of special coloring and options that go there depending on what's going on and get in to get folder than am in and then on the right side on the same line I've got the current date and time and I've got the elapsed execution time of the last command how long did the last thing I run take to run and then you can kind of see in the bottom corner here it turns red if there was an error and it stays green if there wasn't so there's a lot going on right and we're gonna unpack it and the doors are closed so you're stuck so don't even bother looking at your phone either because I'm gonna see you that's my slides let's let's get to it right let me find a better place there we go so this is my prompt again just to show you the bigger version from left to right here and this was my prompt function well more specifically this is my profile so not the biggest profile I've ever seen oh and I've got this at the bottom it's 76 lines of prompt and then there's one line of I miss type code insiders all the time but I want to have it open on the command line so I just named a code so let's ignore that this is my prompt function it's small right like it's pretty easy to digest you guys got it all why are we even doing this talk easy right let's just start at the beginning shall we we're gonna start talking about some of the ansi escape sequences first and i know there's a bunch of global variable there's seven lines of global variables to start this thing they don't all need to be global this is just kind of organized for me to show you in such a way that it's a little bit easier to demo and I can go into the shell and actually use these things rather than have them only available in my prompt you probably don't want these all global right I start by declaring a fork prompt color that is the text color black very sleek fast and black and awesome the reason I'd actually declare that globally is because most of the time I want it to be black but every now and then you'll do a presentation where you're using like light theme or something and or using something that has a white background or a black background or whatever it just doesn't work brightly it's nice to have it variable not blowing any minds I'm sure and then we've got a left arrow and a right arrow that's cool did you guys know you could do this with the character accelerator and give it a hex code that's neat right if I come in here and I go right arrow gives me a right arrow we go left arrow gives me a left arrow cool that's not something that you get with a regular font but that's something that you get with a power line font those highly numbered ASCII characters because that's not typically different than what I did later you go car and then I think like 70 is a printable character I got a capital F whereas if you do character and then this big hex thing the zero X tells you that what's coming next is hex decimal right everybody knows that I'm just printing a character out to the screen so it's fast right like I'm not rendering anything weird I'm just getting a character out of the installed font so to do that you need a power line font installed and I'm using not just the regular shell but you can use the the regular just PowerShell dot exe or p WS HDX II I'm using Kong EMU console emulator and I've changed my settings probably hard to see search for fonts I've changed it console is it's pulling it from somewhere it's one of the powerline fonts you set this up once and you never think about it again so I've got some arrows set up because guess what when you start looking at the prompt itself there's interleaving effect but I'm just kind of set up is done with those arrows and is it relevant to my job no is it something that makes me work better or faster no does it look cool as hell yeah come on you want arrows so arrows and if this is a pain in the ass to look at when you're looking at it in code so it's not a variable to it even though the variables longer than the hex code I think the variable is nicer to look at than the hex code and it just helps me remember what the heck's going on I have the escape character saved their character 27 if I just do it car 27 it's like you just hit escape and then enter right remember they're called ansi escape sequences they begin with the escape character cool right so I've got the escape characters so I don't have to look at that too much and then I've got the sequences that help you denote the foreground and background colors because these things get a little bit squirrely especially if you're cheating ahead and kind of looking at how some of these strings start to look here you go that looks like crap and you'd be like yeah okay good thing you have these these variable names that really helps it but this is another one like where you want to set the foreground color its escape and then an opening square bracket and then 38 semicolon five and then like I said me : and then a number for your color and then the letter M and then the string that you want to put there's rolls off the tongue right yeah so it obviously doesn't so to save myself a little bit of mayhem I store the foreground and background escape sequences in a variable so that I can embed them in a string more conveniently and that's going to come in to play a lot as we go and again where do you find this out the docs page is helpful it tells you exactly what you need to be doing and then I have just like of a string called dollar prompt they just rights out my prompt like that is the content of the prompt because I write that out predictably at the end of my prompt function alright so if you have ever tried to work with a series of elements you'll have discovered that a collection is handy like if you want to say oh I don't need this and I need this and I need this and I needed to go through them all and make a string out of them store them in a collection and you might think I'll store them in an array and that you would have a poorly performing font prompt and you should feel bad about yourself because there's better performing collection now I'm using generic lists here does anybody in my C sharp partial command listings more in a few hands yeah so we played around using different types of collections and there than just regular arrays and so this is a generic list of script locks and it is again global doesn't necessarily have to be but I like screwing with it and this is just kind of for me call prompt right and as the comment very helpfully and completely describes with no more questions it is all the stuff that's right aligned on my prompt so this is this if you can see my gesturing with the mouse this with the time and date stuff and the elapsed time and the stuff that's on the right side is what's coming out of here so let's go through it and it's collection of script locks and as you know you can just execute a script block and get the output out of it so down at the end here we basically just invoke all these script blocks and join the contents together and we'll try to kind of go through it relatively quickly here the first one is gonna change the foreground color to whatever the air color is we'll get to that in a second and then this M tells the Consul that okay this is setting the foreground color for the thing that I'm screwing with and then the zero is just using the format operator to stick in the left arrow you guys remember what left arrow looks like looks like that you guys think you figured out what a left arrow that's got air color of whatever I said it too looks like it looks like that looks like it looks like that where I've just highlighted so that's all that does that incompletely easy to read digestible line of code makes a little blue arrow thank you it's kind of a lot of Dorking around together the left arrow isn't it but it's cool so you like it this error color in the actual prompt function you guys know what this does this is dollar question work all the question mark tells me it says true okay perfect it's a constant no it tells me the last thing that I executed did that complete successfully order to throw an error and if I do something like throw throw throw oops I got an error and now if I look at dollar question mark it says false because the other thing had an error and notice how this turn to red or orange because Connie moon is fun I've got this set if dollar error so if it's true they can give me a color of 22 and if it's not give me a color of one and that is being plugged in here so this line says the foreground color set it to the air color and then this M is basically like a separation that I don't know why it's M it just is and then the zero or sorry the oh no zero I have it right is the left arrow and in so it's being plugged into that so that's what a lot of the rest of this kind of follows there's going to be a bunch of formatting and a string and then a format operator and then the content that I'm actually putting in that thing to be stuck in there after so that knowing that in that pattern that gets followed for most of the rest of this it's going to help the rest of this go faster is you'll kind of understand what's going on so the next thing that isn't an arrow is actually helpful in some ways is we're setting the foreground color to the foreground prompt color so we're just saying black because black text okay and then we're sending the background - whatever the error color is so you'll notice with a lot of these like arrows and interleaving and stuff the foreground of the arrow is gonna be the background of the thing that it kind of comes with and that's more easily highlighted if you look at like this one the one that I highlighted can you guys see that kind of okay this black area is the foreground the yellow area is the background make sense right because I'm printing the arrow character now on like the one that goes beside it the one is in the foreground and then the background I just want to be the same color as the arrow so it's kind of a weird thing to get your head around and I know I'm beating it to death a little bit but that's how this like overlaid effect kind of works right and so like in this see here come here that arrow is the foreground color of this purple where the where the drive letter is and the background color is for what's next because it's supposed to kind of go in between okay okay if you don't get it play with it a little bit and it'll become obvious what screwing with the different colors means so I'm setting the foreground color just do the foreground color of text and the background color to the same foreground color that I set the arrow to because it's kind of a group and then what am i sticking in there this monstrosity this is probably maybe the happiest kind of Foglia sting that I'm doing in in this most of the rest of it is relatively good practice but this is not I'm saying here get history right if I do get history here's all the stuff that have executed in this session and you know something cool if I go action we'll get to that but I'm getting this and if I go get history just take zero you know all the properties it has like an execution status and when it started and when it ended and it tells you how long it did and these are representatives as date/time strings but they have an actual date time object associated with them so I can make a timespan here out of the elapsed time timespan of start to end but I've got a little and I format that it with this C operator string formatting it's not what this is totally super above but and I'm taking the last thing that I ran and I'm getting that timespan and that's what goes in here and most of them are just milliseconds some of them are longer and that saves me from having to go back and check if I wanted to see which one of these performs better well run them both and see which one performs better right and the hacky part that I don't love I'll be right with you is that if I haven't run any commands yet just when you go and create a new window it needs to put something there but there was nothing to put so what happens in my case I just write out zeros because I'm lazy and I want it to look the same and honestly it's kind of just your prompt function so it doesn't really have unit tests and peer review so guess what zero is across the board but why don't you just omit it just don't have it there well when you see how I do the right and left alignment you'll see you'll quickly see why it's important to have something there all the time so that's what's going on with that block kind of starting to roll along here and that's kind of what we got going on there was a question let's look at it again this duration yeah kind of no more questions for anybody else if exist now anybody thinks they're smarter than me KITT now I'm just kidding yeah yeah I see if it was peer-reviewed oh and now that problem so the next line the next two things here so reticular that we're still on the right side of the prompt we've got this gray white section here that just displays the current time in a nice little format that always has this leading zero I should change that to 24-hour time looking at it now but whatever it starts with an arrow as well the foreground color we're setting it to seven for good reasons the background color we're setting to the air color because if you look at this just click one there the background color should match this block the foreground color should match the background of this block and that gives me that little interleaved effect again right okay I'm not going to keep beating it apart foreground color background color and so I'm sticking in another left arrow okay big deal and then I'm doing a get date and I'm formatting it in the way that I wanted to do it format it okay I'm not even gonna run that because I think you know what it looks like and that's the right prompt that's all of this cool right what about the left prompt left prompts where it gets fun it's but one of the get stuff is where it gets fun the left prompt you don't go arrow thin thing than air than thing see you go thing and then arrow and then thing than an arrow so that order is switched and the first thing I'm doing is I'm setting the foreground to the for prompt color you know why do you need a variable for that it's just it's always yellow well it's not always yellow if I open PowerShell it's blue if I we run PowerShell core I have it yellow and that's just a visual indicator for me to say oh yeah if I'm in core I want to know if I'm in 5.1 I want to know that too or just PowerShell wxe and the way I'm doing that is this platform color and I'm just checking for if is Windows then give me 11 and if it's not because 5.1 doesn't have the is Windows variable give me 117 and again these colors are all in a table on the ansible or the ansi escape sequences page right so you know just put in blue and yellow you put in yellow blue you just put in the numbers that correlate to that escape sequence okay so setting the prompt color and the background color to that platform color and then I'm doing this invocation and then the history ID and I'm padding it to be four digits and go why would you want that that seems silly like okay this is line 23 this is line 24 this was line 1 of this session this is line 22 and who cares right well I care because remember when we went get history and I had all these numbers here you know invoke history and then just give it this number what's a good one let's go right arrow I want to replay this line and go 12 and then it tells you what the code was that it executed and then it executes sit and there's a nice little if I go R and then 12 it does the same thing ours just an alias for invoke history right and I like being able to do that sometimes just cuz oh it was way up here oh I could hit the up arrow like an idiot come on now wouldn't you rather replay the line that'd be kind of fun and plus I don't know I figured out how to do it and I kept it so I have that this invocation history ID is just the line number right and I stick that in to this whole big nicely former I think I'm actually doing double string formatting here first I'm taking the history ID and I'm formatting it as a four-digit number that's why I have these leading zeros there's a format it is a four-digit number and then I stuck that into this with the color formatting and everything like that and then a right arrow background colors 22 because that's what's coming next for the background color of the other one and then the foreground color is the platform color which is the background color everyone's confused it great and then I stick a right arrow in there that's this yeah I can't see it cuz I yellow highlights funny but that's this arrow between the 25 and the 1 go next that's the prompt level is one that I find is actually pretty handy when you guys are traversing the file system do you do change directory see temp hmm or do you do push directory see temp oh yeah push location hey I said no more questions push location and you go well they both got you there except one's longer to type even if you use and you know I did that is because usually I use the push D alias for push location if you do a get location tells you where you are okay big whoop if I do a get location stack it gives me all the history of the locations that I pushed not the locations that I change directory into but the ones that I pushed using push location and why would you want to do that because push location has a pop pop location or pop D and it puts you back to the one you were just in and I was just in the same one I pushed and I keep going back in that stack and now if I do a get I don't have one I'll go push the system sure I'll go a push D Tim and you see this is just a folder name we'll get there don't worry and now I'm in a couple different ones I'll just do a get location stocke say I wanted to go back here but these are nowhere near each other and I don't want to type that and I don't want to have to just like CD dot slash dot dot slash dot dot I can just go pop D and now I'm back in the system volume information one push D into this one because that's where I was at and I could pop and push throughout the file system and that's nice I like using that in scripts as well especially where a script will go and change my location like you might run something that goes and sets your location to some random place on the C Drive and you're going well that sucks I wish I could pop my location back to where I was well now you can and this nested prompt level thing is showing me how long that stack is you can see that it's just a little a couple of arrows thing because I'm antsy and then a tube because there were two items in the stack and then I popped and it went down to one and then I pushed and went back up to two that's all that is and I like that because it's visually pleasing for me it doesn't take up a lot of execution time and it's a reminder that I can just get back to wherever I was and more importantly it tells me when I shouldn't waste my time trying to pop back to other stuff and so what that is more color fun and then I get a variable for how big the stack is right and then this character 187 is the those two little arrows right car one eight seven arrows little arrows and it sticks all that in there and then I do a right arrow cool the next one is the present Drive name so you know if you go a dollar PWD you can get the place you were in if you go doc Drive you get a whole bunch of a whole bunch of information about the drive and you can just isolate a name which is handy because if you go get PS Drive there's not just drives you guys probably know this there's not just drives for like your C Drive I've also got like a certificate drive and an environment drive and I can go city cert and now I'm in the search Drive and if I do a directory listing oh it's like the certificate stores on my computer I can go like LS current user and see the different certificate stores that are in there right and so I like having that just to kind of again visual reminder and you notice when I did that the folder that I was in got a little wonky but it still put the arrow there so that's cool so I put that name in there because I like having that and then this last part is just a split path on the present working Drive - lief you guys know you can split plot a split path c10 and it'll just give you that and if you go - leave it'll just give you the end kind of thing you're obviously well familiar with the regular power shell prompt that gives you your entire path and I'm not changing this because I think that's not useful I'm keeping this part because I think it is useful and I like knowing where I am in the file system right it's kind of helpful when you're navigating but I don't want the whole big a string that tells me that it means these system - oh ok goodness just give me the folder name and that's enough for me so that's these two parts there's more now I'm gonna get repository this is my presentation files get repository and you know how I know it's a git repository because of my computer no because this purple master part showed up and this is the name of the branch that I'm in and if I do like a new item test.txt I got that in there you know what let's try different one this one's a little wonky I want devil workstation there we go so that's that alright so the get part I have to recalculate every time that I actually run the prompt and invoke this stuff and I don't necessarily want to do all of these things every time so I've organized this it doesn't necessarily have to be inside this prompt because you just invoked the script locks anyway but it made more sense to me while I was laying it out so the first thing that I wanted to do on line 36 was am I in a git repo or not because if I'm not in to get repo there ain't no point in trying to calculate all the information about a get repo that I want to look at so I do this git config dash L and that shows you like all your get configure information and I cut it off so there's more if you're not in a get branch or it's sorry if you're not in a get repo you don't have any information on this branch so let me just keep it there and at the end here I got these branch Don masters and I got information about that not important this isn't a session I'm good but if I'm not in a git repo I don't get that branch information so does it match branch if it does then I'm going to get repo if not well then don't bother executing it so then they to go if the string get test which is the output of this isn't all empty then I'm good and I just realized that this match is boolean and I'm using a string operator doing whatever I want to get the branch that I'm in I want this get symbolic graph short again not a get session but that word just says master oh and this where it just says master it's because it's just the name of the branch that I'm in again not a session on get but handy I want to get the e status am I ahead or behind in my commit so am I ahead of the upstream where am i ne behind and then this distance is something that gets put together later and so this whole big section within this purple line here is all just assembling that information that gets put and get because if I go hopefully this one will do it new item I've made a new item and see how that little arrow changed colors from purple to kind of like a green yellow pea soup color there's definitely my first try so it just definitely didn't just take the first color I hadn't already used that showed up good on a black background didn't do that see how that's not because if I do a git status now I have a change to that file right and if I do a git and now I have the whole things yellow and that's helpful for me this is really like why I like this problem and if I do a get commit now it goes back to purple but I'm ahead by one I do a git status I don't have anything to commit but I am ahead by one commit so this is telling me that I'm clean I'm all committed but I'm ahead by one and that's what I'm getting here if I was behind it would tell me I was behind if I did another commit it would tell me I had another commit that I was ahead by more etc and so I like that so how do I get that I've got this where I do it the git diff - staged and that is doing me some favors in this area and then I do the git status status - s which is changing the arrow foreground color or the branch background color it gets touched the status does blue get status - s is telling me whether or not I need to color that arrow whereas the get differ staged is telling me if I have things that are staged but not committed right and so the changes the background color of the whole element there and so if it's got a difference to change it to this three color and if not I change it to that five color weed so green purple and then this is for the ahead behind so if you do this get status - SB you get this stupid-ass string that is just pound pound Master dot dot origin / master square bracket a head one what kind of useful know this is sort of Randy could tell that get was developed by Linus Torvalds and he's used to working with string data and Linux wouldn't it be cool if that was like an object he could parse really easily and you could just like take the ahead behind property out of that or like even just pump it into like convert from string and just get magic object conversion that'll be cool what are these pounds for they have a reason does anybody know no I'm sure there's a great reason the why are there three dots in here I don't know anyway enough complaining about that so I have this regular expression that is basically saying let's work backwards this way the end of the line and then before that if there's square brackets with word characters in them right so this is basically saying my ahead one inside of square brackets at the end of the line again not a session on regex not a session on get just kind of something you can do so I have this doing more regular expressions for figuring out if I'm ahead more for if I'm behind and kind of appending those values together to stick them in later in this distance is just kind of the beginning of the string here I'm starting with the right arrow and then if I'm ahead I need to stick that ahead to peace in there this was the like ahead by one there's what this is all coming out to be right because I'm stripping that out of this data I'm stripping this a one out of here right and I know I'm running a little bit a long time but you'll see and then I'm doing the same with behind am I ahead am i behind and this is just again I'm adding to this distance thing this is that distance block this white block is being represented by that distance variable background color I set to white foreground color is set to the foreground prompt color and then the escape sequences are a little bit unique for killing this at the very end here this escape square bracket 0 M says reset stop coloring stuff because otherwise like this would just remain white all the way across because that's the background color so it says after you print that arrow stop doing stuff okay I have that down here is where the important one is and if there's no header behind then just write the right arrow with the purple color or whatever color it's supposed to be the arrow foreground color okay and then the get prompt is you set the background color set the branch background color depending on whether it's green or purple depending if I have commits that I haven't or sorry depending if I have stuff staged that I haven't committed and then put it right arrow and then put the branch name saw the branch name how I got that and then put in the distance which is that light block if it exists right it might be nothing I might be putting nothing in there and so that is how all of the different elements are derived so quickly while there's time I'd take this left side and I just invoke all the script blocks and then I joined them into one big string right because otherwise I'd have the output all on separate lines I don't want that and if there is no get stuff then I don't have to worry about calculating it so there's just a little bit of differences here to not worry about this get prompt variable that i've been appending to in here right this is that list yes and then the right side same thing invoke all the script blocks and join their output because when you invoke one of these things like let's do this one this is just a script block right and Fayed editor it just writes the thing up but if I go dot VOC it screws up my colors because all it does is right and arrow and do some weird stuff but it gives me that purple one with the green background and then it royally jacks up my prompt for the rest of it because and see escape sequences are fun so I'm invoking all those things and then joining them with no separation characters obviously right I've got enough separation characters I've got separation character anxiety so I invoke those and then the only thing left to do is calculate the distance between these two items here because I want to write this stuff out and then I want there to be something and then I want there to be the left side or the right side stuff and I wanted it to be on the same line because I'm fancy the only thing on my actual prompt line is this arrow right well where does that come from I'll show you and so it was 20 so the host dot u I let me just grab this and show you this is the width of your console and watch if I make it smaller number goes down gonna make it bigger number go up guess what else it does ruins my entire life clearing your back it's not a baby but that is telling you the width of your console the width and what well you did a measurement is that is that pixels no I hope not it's columns right like consoles work on mono spaced fonts where each of these things take weight each one of these characters takes up one column right so this is a hundred and twelve columns wide and I know for a fact that my right hand side output is 28 characters you know how I know that because it never changes length because it's always this date time function it's always this elapsed time function and when I don't have a elapsed time I'm filling in zeros because it's important to me that this remains a static width it doesn't have to but you know who knows why I can't just go right hand prompt dot length arrows not so important but it's because all of this stuff where it gets rendered out into the actual numbers and letters counts to the length of your string that's kind of a sake but it makes sense right because you're writing that string out and so the length of the string includes the instructions that you're sending to the console so could I have done a regular expression to strip that stuff out absolutely do I hate myself that much No so that's why I decided to make the peer-review list decision to say this thing is always going to be static zeros if I don't have anything to put in there because this 28 is a magic number you guys are familiar with that term like why is it 28 because it is so I'm calculating the offset here the offset is just the distance between the left side and the right side or more more explicitly between the edge and where I want the right side to end and then this prompt is I'm joining the left side I'm putting the offset in there and I'm putting the right side in there and then I'm putting a newline and that is where that little angle brace comes from that angle brace is this angle brace I put a couple of I put a new line and then I put an angle brace and then I write out my prompt and I get prompt real quick I know we're out of time but if you don't like the idea of screwing around with ansible or ANSI escape sequences Joel Bennett who whose talk you shows not to attend right now I'm sure he appreciates that has written a module called power line which abstracts a lot of that stuff away from you he has another module called pansies which is powershell ansi escape sequences spells pansies and you can go check that out and install those modules and read through his Docs and there's other ways of doing this but my way includes no dependencies not posh get the only dependency to get you have to have get installed it doesn't include any module dependencies or stuff like that so now that I've shown you my goods thank you very much for attending I hope you've learned something creative that you can go and screw with if not for any other good reason then to kind of prank your coworkers you find me on Twitter join the slack of discord and otherwise thank you very much enjoy the rest of your summit and have a good night [Applause] [Music] you
Info
Channel: PowerShell.org
Views: 5,858
Rating: undefined out of 5
Keywords: powershell, windows powershell, techsession, powershell summit
Id: SdQYooRg7Cw
Channel Id: undefined
Length: 46min 3sec (2763 seconds)
Published: Fri May 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.