Don't do that, do this instead: PowerShell worst practices and how to solve them by Chris Gardner

Ғылым және технология

In past years the 3 furies have shared their insights on various things people do wrong in PowerShell. Let's take that one step further and demo all those things and what you should do instead. We'll explain why it's bad and the other way is better, because code without context doesn't help anyone.
A lot of talks about "Best Practice" are aimed at showing you what you should be doing but not many take the approach of showing what you shouldn't be doing and how to change it. This can make it more difficult to grasp as it might not easily relate to how someone is currently working or has been working, showing the wrong way to do things is more approachable as everyone was a beginner at some point and many people made the same mistakes. Taking this one step further and providing reasoning why one approach is the wrong or worse way to handle something can also allow people to make an informed decision on using that approach anyway, if their circumstances require it.
PowerShell Summit videos are recorded on a "best effort" basis. We use a room mic to capture as much room audio as possible, with an emphasis on capturing the speaker. Our recordings are made in a way that minimizes overhead for our speakers and interruptions to our live audience. These recordings are meant to preserve the presentations' information for posterity, and are not intended to be a substitute for attending the Summit in person. These recordings are not intended as professional video training products. We hope you find these videos useful - the equipment used to record these was purchased using generous donations from members of the PowerShell community.

Пікірлер: 25

  • @HarmonicaMustang
    @HarmonicaMustang3 ай бұрын

    Glad to see I follow most of these practices already, though I didn't know about the $array += optimisation, I went back to my old scripts and modified them, they run much better now.

  • @krwhynot
    @krwhynot2 жыл бұрын

    Thank you for this video, I have been using these "proper" Powershell grammar tips to my current scripts and it is helping clean them up. Thanks again!

  • @TheStevenWhiting
    @TheStevenWhiting2 жыл бұрын

    5:17 Aliases, that's what was doing my head in. Still learning, slowly and the amount of people that release their code with aliases is super annoying. I spend after the time working out what they mean. One script I wrote, as I know I'll forget, I even commented what ever single line was doing to make it even more readable. Obviously I know that's not possible for large scripts but still useful for me.

  • @tonyme7426
    @tonyme7426 Жыл бұрын

    Got to love the question on camel case as that is the biggest discussion we have on projects with external developers. Naming conventions.

  • @MuhammadBerki
    @MuhammadBerki Жыл бұрын

    Great session thanks 🙂👍

  • @daviddow5591
    @daviddow55915 жыл бұрын

    For more clarification on the memory usage of the array += thing: arrays are fixed sized collections of a single type. Assume you have an array (let's call it A) with 100 integers. If you want to add another integer to it (lets call this i), then the computer needs to allocate a new chunk of memory (let's call this array B) with enough space for 101 integers, copy A over to B, and then tack on i. So, if you do something like: $array = @(); 1..100 | foreach-object { $array += $_ } then you're asking the computer to allocate new memory 100 times. It isn't bad for small chunks of data, but adding data to the ends of lists is much more performant. If you want to run a head to head comparison, here's a piece of powershell code you can run (tested on v6): $range = 1..1000; $IntList=[System.Collections.Generic.List[int]]::new(); $IntArray = @(); write-output "List method(ms): $((measure-command -expression { $range | %{ $IntList.Add($_)}}).TotalMilliseconds)"; write-output "Array method(ms): $((measure-command -expression {$range | % {$IntArray += $_}}).TotalMilliseconds)"

  • @daviddow5591

    @daviddow5591

    5 жыл бұрын

    Also, don't write code in scripts like the spaghetti I just wrote lol

  • @Chad-Skillable

    @Chad-Skillable

    6 ай бұрын

    Chris talks about this at 22:12

  • @RPG_ash
    @RPG_ash Жыл бұрын

    Anybody know what the talk/video was about his comment at the end by a guy called Mike and WFP? I'd like to watch that.

  • @PhrontDoor
    @PhrontDoor8 ай бұрын

    I half agree with the hungarian notation one (around 14:20ish). Sure you can put other things in the $strName than a string. But if the programmer made it an strName then he likely intends for you to keep it a string so if you change it to an array, you'll break stuff done later on.

  • @Our1stPlanet
    @Our1stPlanet3 жыл бұрын

    All good stuff. Any chance of getting the source code on Github ?

  • @fuleo
    @fuleo5 жыл бұрын

    In vscode, there's "ms" command latency for every command he typed. How do I set that in vscode ?

  • @michaelo147

    @michaelo147

    4 жыл бұрын

    Did you ever figure this out?

  • @citelao

    @citelao

    4 жыл бұрын

    @@michaelo147 It's probably a custom prompt. If you grab and save the current time (globally) in the prompt, you can check that value the next time the prompt appears. If you subtract the two values, you get time elapsed. "Elapsed time PowerShell" and "custom PowerShell prompts" are good starting searches.

  • @michaelo147

    @michaelo147

    3 жыл бұрын

    @@citelao I finally had some time for this and this was absolutely it. Thank you for teaching me something new.

  • @rpfrompr

    @rpfrompr

    2 жыл бұрын

    @@citelao, could you explain? Was that added to him PS prompt?

  • @TheStevenWhiting

    @TheStevenWhiting

    2 жыл бұрын

    @@rpfrompr As mentioned it appears to be related to the Prompt function. Where you can set what the prompt looks like. No clue on the code to make it display the elapsed time though. Someone has probably written a script for it somewhere.

  • @BrianLalonde
    @BrianLalonde5 жыл бұрын

    Is "Select-Object" more readable than "select"? I don't think maximizing syntax increases readability. Zero aliases or positional params improves approachability for neophytes, but it's obscuring noise to anyone with experience trying to see the big picture.

  • @daviddow5591

    @daviddow5591

    5 жыл бұрын

    Well, aliases are good for shorthand at the terminal too, but with powershell core, it looks like aliases are going to be going away to some extent

  • @gareginasatryan6761

    @gareginasatryan6761

    3 жыл бұрын

    I agree. In a copy command it’s pretty obvious what the positional parameters are. copy was probably one of my first commands when I was around 13.

  • @-dash

    @-dash

    Жыл бұрын

    I think the idea is if you adhere to the schema, there's very little chance for ambiguity and almost certainly does anyone revisiting the code a favor. "Select", at a quick glance, could be referring to Select-String, Select-Object, Select-XML, etc.

  • @gorge5412
    @gorge54123 жыл бұрын

    Ouch. His . . . manner-of-speaking . . . is . . . very . . . difficult-to-understand. A few words gets spit out slowly; he stops, pausing, then races through the next fifteen words. Had to bail out after two minutes. IT TOTALLY DISTRACTS FROM CONCENTRATING ON MATERIAL.

  • @TheStevenWhiting

    @TheStevenWhiting

    2 жыл бұрын

    American? He has an accent here in the UK, its probably that that's bothering you.

  • @geroffmilan3328

    @geroffmilan3328

    Жыл бұрын

    @@TheStevenWhiting sounded fine to me too.

  • @go-meditate

    @go-meditate

    Жыл бұрын

    I am a non-native english speaker and I understood everything perfectly fine.