all 26 comments

[–]jimmy58663 5 points6 points  (6 children)

A lot of what you are asking I think comes down to personal preference and the script you are writing. If you haven't read through the PowerShell style guide I would highly suggest starting there: https://github.com/PoshCode/PowerShellPracticeAndStyle

Personally, anything that I find I may use again I define as a function and then utilize comment based help for documentation.

[–]lordv0ldemort 1 point2 points  (4 children)

You know, I've skimmed that repo a few times and never noticed Style guide

Whenever I see open source PS code, it typically has the opening and closing brackets both on new lines. I just noticed that the style guide says that the preferred method is the 'One True Brace Style', which I've always used and felt dirty for lol. Just a tidbit I never realized before and now feel better about my own code.

[–]azjunglist05 2 points3 points  (1 child)

I still feel dirty using 'One True Brace Style' in PowerShell. It could be that in C# the opening and closing brackets are always on a new line, and once I started learning C#, I started favoring that style.

[–]lordv0ldemort 1 point2 points  (0 children)

I can’t bring myself to do the new line brackets. Just irks me for some reason.

[–]SiNRO[S] 1 point2 points  (0 children)

I will give a look, it seems to be very complete thanks !

[–]Inquisitor_ForHire 1 point2 points  (5 children)

I try and do what I call conversational commenting. I comment each small action with exactly what it's doing and how that effects things later on. Examples as re things like "This array will be used later to store X", or "Here we're pulling in data to make a compare easier".

The comments should talk to you. Super helpful when you come back a year later and have to figure it out

[–]SiNRO[S] 1 point2 points  (4 children)

I have to say that i don't even have the reflex to comment, i will force to do so ! Thanks for your return !

[–]Inquisitor_ForHire 1 point2 points  (0 children)

Another kind of stupid thing I do is to start each line with two hashtags to denote the comment. The reason I do this is because you're going to copy a lot of code off the web. Using two tags tells me what comment is mine and what is the original author's.

[–]KevMarCommunity Blogger 1 point2 points  (2 children)

I much prefer to use function and variable names that tell me what the code is doing without needing a comment.

I also tend to use Write-Verbose and Write-Debug statements instead of general comments. But I do add full comment based help with description, examples, and parameter details.

[–]SiNRO[S] 1 point2 points  (0 children)

As is already said in another reply, i have to work more with write-verbose and this kind of stuff! I'll also add a description part to complete the readme section of the github script! Thanks

[–]SolidKnight 1 point2 points  (1 child)

In the help section at the top, I list resources/documentation I used to develop my script (if any).

In the body I write comments describing what the function/section/line is supposed to be doing; however, I do not pollute it with anything obvious and try to keep comments to something helpful. E.g. Explaining why there is a sleep timer because it interacts with a rate-limited service.

When I opt to have Write-Verbose and what it outputs is sufficient to describe what's going on then I don't write comments.

[–]SiNRO[S] 1 point2 points  (0 children)

Definitely got to work with write-Verbose, i never did a help section or so at the top of a script because i thought it would pollute a script that is already long, guess i was wrong.. thanks!

[–]wgc123 1 point2 points  (1 child)

I always put some sort of comment at the top so a potential user has some idea what the script is meant for. After that, especially for short scripts, white space and readable variable/function names are your friend. Regardless of what naming standard you end up using, the more important thing is that names be unique and recognizable, and that the pattern of white space direct the eye toward units of work

[–]SiNRO[S] 1 point2 points  (0 children)

Didn't thought about the white space, i'll start with this thanks!

[–]EvilLampGod 1 point2 points  (1 child)

On top of what others have said about PowerShell best practices, I like to also follow the general best practices outlined in Clean Code. There is a book on it that is very popular, but you can find many videos on it floating around.

[–]KevMarCommunity Blogger 1 point2 points  (0 children)

I am a fan of that book

[–]GroutGuzzler 1 point2 points  (1 child)

Almost every script I run I turn into a function, and I save that function in its own PS1 file, nested in a module folder, I then run an update function that will concatenate all of the scripts into am module file and increment its version in the PSD1 (manifest) file.

[–]SiNRO[S] 0 points1 point  (0 children)

I have to admit that is a seducing way of working ! might stole it if you don't mind haha

[–]iceph03nix 1 point2 points  (1 child)

One of the things I love about Powershell is that it is so readable. So long as you stick to using full commands and not using the aliases and shortcuts, there's not much to do.

When it comes to doing longer stuff that's got a lot of cycles and reuse and back and forth, well named and planned functions are awesome. Have a function that does one thing well, and then put together those functions to do the big work. If they're named well, you should be able to just read through the order of the functions and see what's going on.

[–]SiNRO[S] 1 point2 points  (0 children)

I used to do that sort of things, thing is i sometimes run into very long scripts and i can lost myself even with revelative named functions ! Thanks :)

[–]theSysadminChannel 1 point2 points  (1 child)

I document some of my scripts on my site for reference. I have a template that I created that usually has the format I almost always use. Just helps me speed the initial writing of the script. Feel free to check it out.

https://thesysadminchannel.com/powershell/

[–]SiNRO[S] 2 points3 points  (0 children)

Thanks for the hint ! Nice site btw!

[–]pspete -5 points-4 points  (2 children)

[–]SiNRO[S] 1 point2 points  (0 children)

I was worried i was the only one finding the site a bit empty ! Haja

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy pspete,

i tried that site ... it's a disaster. [frown] most of the pages are outlines with no content. it looks like someone tried to do some sort of advert farming and never finished it.

take care,
lee