Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerShell Best Practices: Simple Scripting

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about Windows PowerShell best practices for simple scripts.

Microsoft Scripting Guy, Ed Wilson, is here. "Heyyyyy! Script it, baby!" The words of Scripting Cmdlet Style continue to echo through the house. If you have not seen the latest Windows PowerShell community video by Sean Kearney, you should definitely check it out. It is really well done, and so far, it has received four thumbs up on You Tube.

When I am working with Windows PowerShell, and the command begins to wrap to the second line, I start thinking about migrating to the Windows PowerShell ISE. If the line goes beyond two lines, I definitely move to the ISE. The reason is because command-line editing is rudimentary in the Windows PowerShell console, and also because the commands begin to be difficult to read. Also, as the length of command increases, my chance of executing the command correctly the first time decreases. If I pollute my Windows PowerShell console history with a bunch of commands that do not work properly, the whole thing becomes an exercise in futility.

Therefore, I view a simple script as only a little different than the Windows PowerShell console itself. It serves the same essential purpose: to allow me to quickly and efficiently execute simple Windows PowerShell commands. Therefore, my best practices for simple scripting are much the same as they are for the Windows PowerShell console:

  1. A simple script is 15 lines or less.
  2. A simple script is straight line execution (no functions).
  3. I do not feel compelled to use full cmdlet names, aliases are permitted.
  4. I do not feel I need to avoid positional parameters.
  5. I do not set strict mode (Set-Strictmode).
  6. I do not generally initialize all variables prior to use.
  7. I do not generally add comment-based Help.
  8. I add single line comments to clarify usage.
  9. I might add a single line comment that illustrates a sample command.
  10. I generally save the simple script with a reasonable name in my script folder.
  11. I do not bother with version, date, or other "header" types of information.
  12. If I had to look something up on MSDN, I will generally paste the URL in a comment.
  13. Often I will use the ISE to write a quick one-off script, and not save the script. In these cases, it is exactly like using the Windows PowerShell console, except that I have a bit better syntax highlighting.
  14. I use the Script Browser to find sample script, but do not use the Script Analyzer.
  15. I avoid end-of-line comments. Instead, I place comments on the line above the command to be commented, and I do so at the beginning of each line.
  16. I line up related objects and pieces of syntax; therefore, I pay attention to good formatting.
  17. I generally break each line of script at the pipeline character. Most of the time, I do not have more than one pipeline character on a single line.
  18. I leave pipeline characters to the right of the script.
  19. I will generally pipe to a Foreach-Object, instead of storing in a variable and using the ForEach statement to walk the collection.
  20. I always prefer a Windows PowerShell cmdlet to a COM object or to creating a .NET Framework class and calling methods and properties from that.
  21. I store credentials to a variable, and I use it during my Windows PowerShell sessions in the ISE.
  22. I often close the Windows PowerShell ISE to ensure I am not inadvertently taking advantage of an object created during a different scripting session. This is the best way to clear variables; remove objects; and release PS Drives, COM objects, and other scripting paraphernalia.

That is an introduction to Windows PowerShell best practices related to quick scripts. Best Practices Week will continue tomorrow when I will talk about functions.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>