diff --git a/Best Practices/Introduction.md b/Best Practices/Introduction.md deleted file mode 100644 index 6657f3b..0000000 --- a/Best Practices/Introduction.md +++ /dev/null @@ -1,23 +0,0 @@ -# PowerShell Best Practices - -## Foreword - -If you scan through code projects on [PoshCode](http://PoshCode.org) or the [Technet ScriptCenter Gallery](http://gallery.technet.microsoft.com/scriptcenter), it will be immediately apparent that people in the PowerShell community have vastly different ideas about what's "right and wrong" in the world of PowerShell scripting. - -Over the years several attempts have been made to arrive at a consensus, most notably the "Great Debate" series of blog posts on [PowerShell.org](http://powershell.org/wp/category/great-debates/) following the 2013 Scripting Games, which outlined some of the more controversial issues and asked for community discussions. - -This project has been created, in part, as a public place to continue those debates as well as to document the consensus of the community when we _do_ arrive at a consensus. - -Remember that best practices are not hard-and-fast rules. We don't even consider them as solid as the style guide rules. They are the things you should _usually_ do as a starting point, and should deviate from when it's appropriate. - -One final note about these Best Practices: the perspective of these guidelines has so-far been strongly influenced by system administrator practitioners, less-so by language geeks and developers. We're trying to balance that and provide perspective, but when it comes to best practices, we definitely allow the experience of administrators to drive what the PowerShell community considers best and worst practices -- so if you are working from a different perspective, you'll have to take all of this with a grain of salt. - -## Table of Contents - -- [Naming Conventions](Naming Conventions.md) -- [Building Reusable Tools](Building Reusable Tools.md) -- [Output and Formatting](Output and Formatting.md) -- [Error Handling](Error Handling.md) -- [Performance](Performance.md) -- [Language, Interop and .Net](Language, Interop and .Net.md) -- [Metadata, Versioning, and Packaging](Metadata, Versioning, and Packaging.md) \ No newline at end of file diff --git a/Best Practices/Naming Conventions.md b/Best Practices/Naming Conventions.md deleted file mode 100644 index 6f23cea..0000000 --- a/Best Practices/Naming Conventions.md +++ /dev/null @@ -1,2 +0,0 @@ -TODO: Copy #36 Capitalization guidelines -TODO: Copy #23 Command Prefixes \ No newline at end of file diff --git a/Best Practices/Building Reusable Tools.md b/Best-Practices/Building-Reusable-Tools.md similarity index 88% rename from Best Practices/Building Reusable Tools.md rename to Best-Practices/Building-Reusable-Tools.md index 893e693..47c5fa9 100644 --- a/Best Practices/Building Reusable Tools.md +++ b/Best-Practices/Building-Reusable-Tools.md @@ -2,20 +2,18 @@ For this discussion, it's important to have some agreed-upon terminology. While the terminology here isn't used universally, the community generally agrees that several types of "script" exist: -1. Some scripts contain tools, when are meant to be reusable. These are typically functions or advanced functions, and they are typically contained in a script module or in a function library of some kind. These tools are designed for a high level of re-use. -2. Some scripts are controllers, meaning they are intended to utilize one or more tools (functions, commands, etc) to automate a specific business process. A script is not intended to be reusable; it is intended to make use of reuse by leveraging functions and other commands +1. Some scripts contain tools, which are meant to be reusable. These are typically functions or advanced functions, and they are typically contained in a script module or in a function library of some kind. These tools are designed for a high level of reuse. +2. Some scripts are controllers, meaning they are intended to utilize one or more tools (functions, commands, etc) to automate a specific business process. A controller script is not intended to be reusable; it is intended to make use of reuse by leveraging functions and other commands. For example, you might write a "New-CorpUser" script, which provisions new users. In it, you might call numerous commands and functions to create a user account, mailbox-enable them, provision a home folder, and so on. Those discrete tasks might also be used in other processes, so you build them as functions. The script is only intended to automate that one process, and so it doesn't need to exhibit reusability concepts. It's a standalone thing. -Controllers, on the other hand, often produce output directly to the screen (when designed for interactive use), or may log to a file (when designed to run unattended). - +Controllers often produce output directly to the screen (when designed for interactive use), or may log to a file (when designed to run unattended). # TOOL-02 Make your code modular Generally, people tend to feel that most working code - that is, your code which does things - should be modularized into functions and ideally stored in script modules. -That makes those functions more easily re-used. Those functions should exhibit a high level of reusability, such as accepting input only via parameters and producing output only as objects to the pipeline - +That makes those functions more easily reused. Those functions should exhibit a high level of reusability, such as accepting input only via parameters and producing output only as objects to the pipeline. # TOOL-03 Make tools as re-usable as possible @@ -29,9 +27,9 @@ You can get a list of the verbs by typing 'get-verb' at the command line. # TOOL-05 Use PowerShell standard parameter naming -Tools should be consistent with PowerShell native cmdlets in regards parameter naming. +Tools should be consistent with PowerShell native cmdlets in regard to parameter naming. -For example, use $ComputerName and $ServerInstance rather than something like $Param_Computer or $InstanceName +For example, use $ComputerName and $ServerInstance rather than something like $Param_Computer or $InstanceName. # TOOL-06 Tools should output raw data @@ -45,7 +43,6 @@ For example, a function named Get-DiskInfo would return disk sizing information An intermediate step is useful for tools that are packaged in script modules: views. By building a manifest for the module, you can have the module also include a custom .format.ps1xml view definition file. The view can specify manipulated data values, such as the default view used by PowerShell to display the output of Get-Process. The view does not manipulate the underlying data, leaving the raw data available for any purpose. - # WAST-01 Don't re-invent the wheel There are a number of approaches in PowerShell that will "get the job done." In some cases, other community members may have already written the code to achieve your objectives. If that code meets your needs, then you might save yourself some time by leveraging it, instead of writing it yourself. @@ -77,16 +74,12 @@ It has been argued by some that, "I didn't know such-and-such existed, so I wrot On the flip side, it's important to note that writing your own code from the ground up can be useful if you are trying to learn a particular concept, or if you have specific needs that are not offered by another existing solution. - # WAST-02 Report bugs to Microsoft -An exception: if you know that a built-in technique doesn't work properly (e.g., it is buggy or doesn't accomplish the exact task), then obviously it's fine to re-invent the wheel. However, in cases where you're doing so to avoid a bug or design flaw, then you should - as an upstanding member of the community - report the bug on Connect.Microsoft.com also. - - +An exception: if you know that a built-in technique doesn't work properly (e.g., it is buggy or doesn't accomplish the exact task), then obviously it's fine to re-invent the wheel. However, in cases where you're doing so to avoid a bug or design flaw, then you should - as an upstanding member of the community - report the bug on [github.com/powershell](https://github.com/PowerShell/PowerShell/issues) also. TODO: The "PURE" section is dubious at best. We need to discuss it. - # PURE-01 Use native PowerShell where possible This means not using COM, .NET Framework classes, and so on when there is a native Windows PowerShell command or technique that gets the job done. @@ -104,4 +97,3 @@ Document the reason for using tools other than PowerShell in your comments. That said, you truly become a better PowerShell person if you take the time to wrap a less-preferred way in an advanced function or cmdlet. Then you get the best of both worlds: the ability to reach outside the shell itself for functionality, while keeping the advantages of native commands. Ignorance, however, is no excuse. If you've written some big wrapper function around Ping.exe simply because you were unaware of Test-Connection, then you've wasted a lot of time, and that is not commendable. Before you move on to a less-preferred approach, make sure the shell doesn't already have a way to do what you're after. - diff --git a/Best Practices/Error Handling.md b/Best-Practices/Error-Handling.md similarity index 83% rename from Best Practices/Error Handling.md rename to Best-Practices/Error-Handling.md index e4ab42e..46c627f 100644 --- a/Best Practices/Error Handling.md +++ b/Best-Practices/Error-Handling.md @@ -2,9 +2,9 @@ When trapping an error, try to use -ErrorAction Stop on cmdlets to generate terminating, trappable exceptions. -# ERR-02 Use $ErrorActionPreference='Stop' or 'Continue' when calling non-cmdlets +# ERR-02 Use $ErrorActionPreference = 'Stop' or 'Continue' when calling non-cmdlets -When executing something other than a cmdlet, set $ErrorActionPreference='Stop' before executing, and re-set to Continue afterwards. If you're concerned about using -ErrorAction because it will bail on the entire pipeline, then you've probably over-constructed the pipeline. Consider using a more scripting-construct-style approach, because those approaches are inherently better for automated error handling. +When executing something other than a cmdlet, set $ErrorActionPreference = 'Stop' before executing, and re-set to Continue afterwards. If you're concerned about using -ErrorAction because it will bail on the entire pipeline, then you've probably over-constructed the pipeline. Consider using a more scripting-construct-style approach, because those approaches are inherently better for automated error handling. Ideally, whatever command or code you think might bomb should be dealing with one thing: querying one computer, deleting one file, updating one user. That way, if an error occurs, you can handle it and then get on with the next thing. @@ -56,7 +56,7 @@ $user = Get-ADUser -Identity DonJ if ($user) { $user | Do-Something -} else [ +} else { Write-Warning "Could not get user $user" } ``` diff --git a/Best-Practices/Introduction.md b/Best-Practices/Introduction.md new file mode 100644 index 0000000..ca8cad6 --- /dev/null +++ b/Best-Practices/Introduction.md @@ -0,0 +1,25 @@ +# PowerShell Best Practices + +## Foreword + +If you scan through code projects on [PoshCode](https://github.com/PoshCode) or the [Microsoft Learn Code Samples Gallery](https://learn.microsoft.com/en-us/samples/browse/?languages=powershell), it will be immediately apparent that people in the PowerShell community have vastly different ideas about what's "right and wrong" in the world of PowerShell scripting. + +Over the years several attempts have been made to arrive at a consensus, most notably the "Great Debate" series of blog posts on [PowerShell.org](https://powershell.org/?s=great+debate) following the 2013 Scripting Games, which outlined some of the more controversial issues and asked for community discussions. + +This project has been created, in part, as a public place to continue those debates as well as to document the consensus of the community when we _do_ arrive at a consensus. + +Remember that best practices are not hard-and-fast rules. We don't even consider them as solid as the [Style Guide](../Style-Guide/Introduction.md) rules. They are the things you should _usually_ do as a starting point, and should deviate from when it's appropriate. + +One final note about these Best Practices: the perspective of these guidelines has so-far been strongly influenced by system administrator practitioners, less-so by language geeks and developers. We're trying to balance that and provide perspective, but when it comes to best practices, we definitely allow the experience of administrators to drive what the PowerShell community considers best and worst practices -- so if you are working from a different perspective, you'll have to take all of this with a grain of salt. + +## Table of Contents + +- [Naming Conventions](Naming-Conventions.md) +- [Building Reusable Tools](Building-Reusable-Tools.md) +- [Writing Parameter Blocks](Writing-Parameter-Blocks.md) +- [Output and Formatting](Output-and-Formatting.md) +- [Error Handling](Error-Handling.md) +- [Performance](Performance.md) +- [Security](Security.md) +- [Language, Interop, and .NET](Language-Interop-and-.NET.md) +- [Metadata, Versioning, and Packaging](Metadata-Versioning-and-Packaging.md) diff --git a/Best Practices/Language, Interop and .Net.md b/Best-Practices/Language-Interop-and-.NET.md similarity index 96% rename from Best Practices/Language, Interop and .Net.md rename to Best-Practices/Language-Interop-and-.NET.md index e5df50b..f0d697b 100644 --- a/Best Practices/Language, Interop and .Net.md +++ b/Best-Practices/Language-Interop-and-.NET.md @@ -30,7 +30,7 @@ The `#requires` statement will prevent the script from running on the wrong vers ### PowerShell Supported Version -When working in an environment where there are multiple versions of PowerShell make sure to specify the lowest version your script will support by prividing a Requires statement at the top of the script. +When working in an environment where there are multiple versions of PowerShell make sure to specify the lowest version your script will support by providing a Requires statement at the top of the script. ```PowerShell #Requires -Version 2.0 diff --git a/Best Practices/Metadata, Versioning, and Packaging.md b/Best-Practices/Metadata-Versioning-and-Packaging.md similarity index 100% rename from Best Practices/Metadata, Versioning, and Packaging.md rename to Best-Practices/Metadata-Versioning-and-Packaging.md diff --git a/Best-Practices/Naming-Conventions.md b/Best-Practices/Naming-Conventions.md new file mode 100644 index 0000000..cabba4a --- /dev/null +++ b/Best-Practices/Naming-Conventions.md @@ -0,0 +1,3 @@ +TODO: Copy [#36](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/36) Capitalization guidelines + +TODO: Copy [#23](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/23) Command Prefixes diff --git a/Best Practices/Output and Formatting.md b/Best-Practices/Output-and-Formatting.md similarity index 64% rename from Best Practices/Output and Formatting.md rename to Best-Practices/Output-and-Formatting.md index 115222f..d1a7fc0 100644 --- a/Best Practices/Output and Formatting.md +++ b/Best-Practices/Output-and-Formatting.md @@ -2,11 +2,11 @@ TODO: This whole document is STILL ROUGH DRAFT -## Don't use write-host unless you really mean it +## Don't use Write-Host unless you really mean it Previous to PowerShell 5, Write-Host has no functionality at all in non-interactive scripts. It cannot be captured or redirected, and therefore should only be used in functions which are "Show"ing or "Format"ing output, or to display something as part of an interactive prompt to the user. -That is: you should not use Write-Host to create script output unless your script (or function, or whatever) uses the Show verb (as in, Show-Performance) or the Format verb (as in, Format-Hex), or has a `-Formatted` switch parameter. You may also use it to build a interactions with the user in other cases (e.g. to write extra information to the screen before prompting the user for a choice or input). +That is: you should not use Write-Host to create script output unless your script (or function, or whatever) uses the Show verb (as in, Show-Performance) or the Format verb (as in, Format-Hex), or has a `-Formatted` switch parameter. You may also use it to build interactions with the user in other cases (e.g., to write extra information to the screen before prompting the user for a choice or input). Generally, you should consider the other Write-* commands first when trying to give information to the user. @@ -14,7 +14,7 @@ Generally, you should consider the other Write-* commands first when trying to g When you're letting the user know how far through the script they are, or just making sure they know that _something_ is happening, Write-Progress is the right command to use. In the case of graphical hosts or remote jobs, this output can be shown to the user in real time, even when verbose and other streams are being collected and logged. -However, progress output is ephemeral -- it doesn't stick around, and you should not put anything exclusively in the progress stream that the user _needs_ to see, or might want to review after the script finishes. +Progress output is ephemeral, however, in that it doesn't stick around. You should not put anything exclusively in the progress stream that the user _needs_ to see, or might want to review after the script finishes. ## Use Write-Verbose to give details to someone running your script @@ -22,17 +22,17 @@ You should use verbose output for information that contains details about the va ## Use Write-Debug to give information to someone maintaining your script -You should use the debug output stream for output that is useful for script debugging ("Now entering main loop," "Result was null, skipping to end of loop"), or to display the value of a variable before a conditional statement, so the maintainer can break into the debugger if necessary. +You should use the debug output stream for output that is useful for script debugging (e.g., "Now entering main loop" or "Result was null, skipping to end of loop"), or to display the value of a variable before a conditional statement, so the maintainer can break into the debugger if necessary. > TIP: When debugging you should be aware that you can set `$DebugPreference = "Continue"` to see this information on screen without entering a breakpoint prompt. ## Use CmdletBinding if you are using output streams -As we've already written elsewhere, you should probably [always use CmdletBinding](https://github.com/PoshCode/PowerShellPracticeAndStyle/blob/master/Style%20Guide/Code%20Layout%20and%20Formatting.md#always-write-cmdletbinding). +As we've already written elsewhere, you should probably [always use CmdletBinding](../Style-Guide/Code-Layout-and-Formatting.md#always-start-with-cmdletbinding). -However, it's particularly important when you're using Write-Verbose and Write-Debug, as the Verbose and Debug output streams are off by default, and the `[CmdletBinding()]` attribute enables the common `-Verbose` and `-Debug` switches which turn those streams on. +Using CmdletBinding is particularly important, however, when you're using Write-Verbose and Write-Debug, as the Verbose and Debug output streams are off by default, and the `[CmdletBinding()]` attribute enables the common `-Verbose` and `-Debug` switches which turn those streams on. -It also enables the switches for the Warning and Error streams, as well as ways of collecting those streams into variables. You should read the [always use CmdletBinding](https://github.com/PoshCode/PowerShellPracticeAndStyle/blob/master/Style%20Guide/Code%20Layout%20and%20Formatting.md#always-write-cmdletbinding) topic for more information. +CmdletBinding also enables the switches for the Warning and Error streams, as well as ways of collecting those streams into variables. You should read the [always use CmdletBinding](../Style-Guide/Code-Layout-and-Formatting.md#always-start-with-cmdletbinding) topic for more information. ## Use Format Files for your custom objects @@ -44,11 +44,11 @@ You should avoid mixing different types of objects in the output of a single com For the sake of tools and command-search, you should indicate with the `[OutputType()]` attribute the output type(s) of your scripts, functions or cmdlets (see about_Functions_OutputTypeAttribute for more information). -When you do combine the output of multiple types objects, they should generally be derived from a common basetype (like FileInfo and DirectoryInfo come from System.IO.FileSystemInfo), or should have format or type files which cause them to output the same columns. In particular, you must avoid outputting strings interspersed in your output. +When you combine the output of multiple types objects, they should generally be derived from a common basetype (like FileInfo and DirectoryInfo come from System.IO.FileSystemInfo), or should have format or type files which cause them to output the same columns. In particular, you must avoid outputting strings interspersed in your output. ### Two important exceptions to the single-type rule -**For internal functions.** it's ok to return multiple different types because they won't be "output" to the user/host, and can offer significant savings (e.g. one database call with three table joins, instead of three database calls with two or three joins each). You can then call these functions and assign the output to multiple variables, like so: +**For internal functions** it's ok to return multiple different types because they won't be "output" to the user/host, and can offer significant savings (e.g., one database call with three table joins, instead of three database calls with two or three joins each). You can then call these functions and assign the output to multiple variables, like so: ```PowerShell $user, $group, $org = Get-UserGroupOrg diff --git a/Best Practices/Performance.md b/Best-Practices/Performance.md similarity index 64% rename from Best Practices/Performance.md rename to Best-Practices/Performance.md index 98bac45..0b0a50e 100644 --- a/Best Practices/Performance.md +++ b/Best-Practices/Performance.md @@ -2,14 +2,16 @@ PowerShell comes equipped with 3.2 million performance quirks. Approximately. -For example, the first line below executes a lot faster than the second: +If you're aware of multiple techniques to accomplish something, and you're writing a production script that will be dealing with large data sets (meaning performance will become a cumulative factor), then test the performance using Measure-Command or the Profiler module, or some other tool. + +For example: ```PowerShell -[void]Do-Something -Do-Something | Out-Null +foreach ($result in Do-Something) { $result.PropertyOne + $result.PropertyTwo } +Do-Something | ForEach-Object { $_.PropertyOne + $_.PropertyTwo } ``` -If you're aware of multiple techniques to accomplish something, and you're writing a production script that will be dealing with large data sets (meaning performance will become a cumulative factor), then test the performance using Measure-Command or some other tool. +In this case, the `foreach` language construct is faster than piping to the `ForEach-Object` cmdlet -- but the point is that you should measure, and do so on the hardware and PowerShell version where the performance matters to you. # PERF-02 Consider trade-offs between performance and readability @@ -20,10 +22,10 @@ This is an important area for people in the PowerShell community. While everyone For example: ```PowerShell -$content = Get-Content file.txt +$content = Get-Content -Path file.txt -ForEach ($line in $content) { - Do-Something -input $line +foreach ($line in $content) { + Do-Something -Input $line } ``` @@ -32,38 +34,47 @@ Most folks will agree that the basic aesthetics of that example are good. This s Now consider this alternate approach: ```PowerShell -Get-Content file.txt | +Get-Content -Path file.txt | ForEach-Object -Process { - Do-Something -input $\_ + Do-Something -Input $_ } ``` -As described elsewhere in this guide, many folks in the community would dislike this approach for aesthetic reasons. However, this approach has the advantage of utilizing PowerShell's pipeline to "stream" the content in file.txt. Provided that the fictional "Do-Something" command isn't blocking the pipeline (a la Sort-Object), the shell can send lines of content (String objects, technically) through the pipeline in a continuous stream, rather than having to buffer them all into memory. +As described elsewhere in this guide, many folks in the community would dislike this approach for aesthetic reasons. However, this approach has the advantage of utilizing PowerShell's pipeline to "stream" the content in file.txt. Provided that the fictional "Do-Something" command isn't blocking the pipeline (à la Sort-Object), the shell can send lines of content (String objects, technically) through the pipeline in a continuous stream, rather than having to buffer them all into memory. Some would argue that this second approach is always a poor one, and that if performance is an issue then you should devolve from a PowerShell-native approach into a lower-level .NET Framework approach: ```PowerShell -$sr = New-Object -Type System.IO.StreamReader -Arg file.txt +$sr = New-Object -TypeName System.IO.StreamReader -ArgumentList file.txt while ($sr.Peek() -ge 0) { - $line = $sr.ReadLine() - Do-Something -input $line + $line = $sr.ReadLine() + Do-Something -Input $line } ``` There are myriad variations to this approach, of course, but it solves the performance problem by reading one line at a time, instead of buffering the entire file into memory. It maintains the structured programming approach of the first example, at the expense of using a potentially harder-to-follow .NET Framework model instead of native PowerShell commands. Many regard this third example as an intermediate step, and suggest that a truly beneficial approach would be to write PowerShell commands as "wrappers" around the .NET code. For example (noting that this fourth example uses fictional commands by way of illustration): ```PowerShell -$handle = Open-TextFile file.txt +$handle = Open-TextFile -Path file.txt -while (-not Test-TextFile -handle $handle) { - Do-Something -input (Read-TextFile -handle $handle) +while (-not (Test-TextFile -Handle $handle)) { + Do-Something -Input (Read-TextFile -Handle $handle) } ``` -This example reverts back to a native PowerShell approach, using commands and parameters. The proposed commands (Open-TextFile, Test-TextFile, and Read-TextFile) are just wrappers around .NET Framework classes, such as the StreamReader class shown in the third example. +This example reverts back to a native PowerShell approach, using commands and parameters. The proposed commands (`Open-TextFile`, `Test-TextFile`, and `Read-TextFile`) are just wrappers around .NET Framework classes, such as the StreamReader class shown in the third example. You will generally find that it is possible to conform with the community's general aesthetic preferences while still maintaining a good level of performance. Doing so may require more work - such as writing PowerShell wrapper commands around underlying .NET Framework classes. Most would argue that, for a tool that is intended for long-term use, the additional work is a worthwhile investment. -The moral here is that both aesthetic and performance are important considerations, and without some work context, neither is inherently more important than the other. It is often possible, with the right technique, to satisfy both. As a general practice, you should avoid giving up on aesthetics solely because of performance concesrns - when possible, make the effort to satisfy both performance and aesthetics. +The moral here is that both aesthetic and performance are important considerations, and without some work context, neither is inherently more important than the other. It is often possible, with the right technique, to satisfy both. As a general practice, you should avoid giving up on aesthetics solely because of performance concerns - when possible, make the effort to satisfy both performance and aesthetics. + +# PERF-03 Language > Framework > Script > Pipeline + +This is just a rough guideline, but as a general rule: + +1. Language features are faster than features of the .NET Framework +2. Compiled methods on objects and .NET classes are still faster than script +3. Simple PowerShell script is still faster than calling functions or cmdlets +It's counter-intuitive that script is faster than calling cmdlets that are compiled, but it's frequently true, unless there is a lot of work being done by each cmdlet. The overhead of calling cmdlets and passing data around is significant. Of course, this is just a guideline, and you should always **measure**. diff --git a/Best Practices/Security.md b/Best-Practices/Security.md similarity index 88% rename from Best Practices/Security.md rename to Best-Practices/Security.md index e70f0ce..50c4472 100644 --- a/Best Practices/Security.md +++ b/Best-Practices/Security.md @@ -16,11 +16,11 @@ param ( ) ``` -If you absolutely must pass a password in a plain string to a .Net API call or a third party library it is better to decrypt the credential as it is being passed instead of saving it in a variable. +If you absolutely must pass a password in a plain string to a .NET API call or a third party library, it is better to decrypt the credential as it is being passed instead of saving it in a variable. ```PowerShell # Get the cleartext password for a method call: - $Insecure.SetPassword( $Credentials.GetNetworkCredential().Password ) + $Insecure.SetPassword($Credentials.GetNetworkCredential().Password) ``` #### Other Secure Strings @@ -32,10 +32,10 @@ Note, if you ever need to turn a SecureString into a string, you can use this me ```PowerShell # Decrypt a secure string. - $BSTR = [System.Runtime.InteropServices.marshal]::SecureStringToBSTR($this); - $plaintext = [System.Runtime.InteropServices.marshal]::PtrToStringAuto($BSTR); - [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR); - return $plaintext + $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($this) + $plaintext = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) + [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) + $plaintext ``` * For credentials that need to be saved to disk, serialize the credential object using @@ -63,4 +63,3 @@ computer where it was generated. # Read the Standard String from disk and convert to a SecureString $Secure = Get-Content -Path "${Env:AppData}\Sec.bin" | ConvertTo-SecureString ``` - diff --git a/Best Practices/TODO.md b/Best-Practices/TODO.md similarity index 74% rename from Best Practices/TODO.md rename to Best-Practices/TODO.md index fae2840..2fe8cd6 100644 --- a/Best Practices/TODO.md +++ b/Best-Practices/TODO.md @@ -1,7 +1,6 @@ These documents are in an extremely rough state, not suitable for inclusion in the main guide yet. -### Using The .Net Framework - +### Using the .NET Framework - [Control what gets exported in a module](#control-what-gets-exported-in-a-module) - [Specify when to use a Manifest for a module](#specify-when-to-use-a-manifest-for-a-module) @@ -42,37 +41,27 @@ These documents are in an extremely rough state, not suitable for inclusion in t - [AVOID appending to arrays in a loop](#avoid-appending-to-arrays-in-a-loop) - [EXCEPTIONS:](#exceptions) - [RATIONALE:](#rationale) - - [AVOID appending to string in a loop](#avoid-appending-to-string-in-a-loop) - - [EXCEPTIONS:](#exceptions-1) - - [RATIONALE:](#rationale-1) - [Strongly type parameters](#strongly-type-parameters) - [Don't reinvent the wheel](#dont-reinvent-the-wheel) - [Let's talk about Logging](#lets-talk-about-logging) - [Let's talk about code signing](#lets-talk-about-code-signing) -- [Don't reinvent the wheel](#dont-reinvent-the-wheel-1) -- [Let's talk about Logging](#lets-talk-about-logging-1) -- [Let's talk about code signing](#lets-talk-about-code-signing-1) - - TODO #### Control what gets exported in a module #### Specify when to use a Manifest for a module - #### Use RequiredAssemblies rather than Add-Type #### Use Add-Type rather than Reflection Avoid using `[System.Reflection]` to load assemblies when possible. Particularly avoid `LoadWithPartialName` (specify the full name instead). #### Use Add-Type for small classes or PInvoke calls -TODO: Is this better than PowerShell Classes, for compatibility? +TODO: Is this better than PowerShell classes, for compatibility? #### Prefer shipping binaries over large compilations -With PowerShell 5, security is tighter, and compiling code in memory will be frowned upon. Now that we have PowerShellGet and the PowerShell Gallery, there are few reasons 's no reason to avoid binaries. - -TODO: Discuss: when does embedding C# code makes sense more sense than just compiling it every time? +With PowerShell 5, security is tighter, and compiling code in memory will be frowned upon. Now that we have PowerShellGet and the PowerShell Gallery, there are few reasons or no reason to avoid binaries. +TODO: Discuss: when does embedding C# code makes more sense than just compiling it every time? ### Performance @@ -81,10 +70,10 @@ Prefer foreach(){} over ForEach-Object Prefer .foreach and .where over cmdlets Prefer functions with process blocks over ForEach-Object -#### Know when to use .Net -Prefer writing wrapper functions to just calling .Net APIs +#### Know when to use .NET +Prefer writing wrapper functions to just calling .NET APIs Discuss: System.IO file loading vs Get-Content (large files) -Discuss: Other examples of .Net API calls that are clearly faster? +Discuss: Other examples of .NET API calls that are clearly faster? Discuss: Casting for creating objects ### Error Handling @@ -94,7 +83,6 @@ Discuss: Avoid depending on `$?` -- why? Discuss: Never use `$Error` in scripts (always use -ErrorVariable) Discuss: Interactively, always copy $Error[0] to $e or something - ### General Design Principles #### Use custom objects @@ -112,26 +100,24 @@ Discuss: During development, always write scripts, which are automatically re-pa This is in the Style Guide too, but we should discuss it in more depth here, and link to it from the Style Guide. Scripts should start life as something like this: ``` -[CmdletBinding()]param() +[CmdletBinding()] +param() process{} end{} ``` You can always ignore one of the blocks, and add parameters and such, but you should never write a script without CmdletBinding, and you should never write one without at least considering making it take pipeline input - ### Include Help TODO: Link to StyleGuide about formatting help -Discuss: Minimum acceptable comment based help: Synopsis, Parameters, and an example for each parameter set (plus pipeline examples if you can contrive one) +Discuss: Minimum acceptable comment-based help: Synopsis, Parameters, and an example for each parameter set (plus pipeline examples if you can contrive one) Discuss: Benefits of MAML help files -#### Always ship a about_ModuleName with modules +#### Always ship an about_ModuleName with modules Discuss: Other reasons to write about_topics - - #### Prefer PipelineByPropertyName parameters. Discuss: This allows the most flexibility: piping objects and using scriptblocks to shape it for parameters. Unless you absolutely need to write a `begin` block and use this parameter in it, you probably should accept it on the pipeline. @@ -146,7 +132,7 @@ You can use aliases to map parameters to property names of objects which might b Particularly when splatting PSBoundParameters to the next function, if that function isn't `[CmdletBinding()]` (it should be!) you must remember to strip the common parameters if they're present. #### Specify positional parameters, but don't use them -When writing at the command line, positional parameters are a blessing, but they can be confusing for future readers. You should always expose your parameters positionally when it makes sense, but you should rarely share a script that pases parameters positionally. +When writing at the command line, positional parameters are a blessing, but they can be confusing for future readers. You should always expose your parameters positionally when it makes sense, but you should rarely share a script that passes parameters positionally. #### Specify short aliases, but don't use them Again, for the sake of typing, it's particularly useful if you can provide two-letter aliases for each of your parameters such that every parameter has a two-letter or less name which is unique. @@ -158,35 +144,32 @@ It doesn't do anything, and it confuses future readers. When prompted for a mandatory parameter, a user can request HelpText, but can't look at the documentation. It's frequently useful to duplicate at least the first sentence or two of the parameter help. ``` -[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$true, -ValueFromPipelineByPropertyName=$true, HelpText='The name of the file to read')] +[Parameter(Position = 1, Mandatory = $true, ValueFromPipeline = $true, +ValueFromPipelineByPropertyName = $true, HelpText = 'The name of the file to read')] [Alias('PSPath','FullName','Path')] [String]$File ``` - #### Use PsCmdlet.ThrowTerminatingError rather than throw #### Use PsCmdlet.WriteError rather than Write-Error Discuss: These need example output to explain why they're better Discuss: a common template (from the Microsoft team) for throwing errors ``` -# Utility to throw an errorrecord -function ThrowError -{ - param - ( - [parameter(Mandatory = $true)] +# Utility to throw an error record +function ThrowError { + param( + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.Management.Automation.PSCmdlet] $CallerPSCmdlet, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ExceptionName, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ExceptionMessage, @@ -194,42 +177,42 @@ function ThrowError [System.Object] $ExceptionObject, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $ErrorId, - [parameter(Mandatory = $true)] + [Parameter(Mandatory = $true)] [ValidateNotNull()] [System.Management.Automation.ErrorCategory] $ErrorCategory ) - $exception = New-Object $ExceptionName $ExceptionMessage; + $exception = New-Object -TypeName $ExceptionName -ArgumentList $ExceptionMessage $errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $ErrorId, $ErrorCategory, $ExceptionObject $CallerPSCmdlet.ThrowTerminatingError($errorRecord) } ``` - #### Use SupportsShouldProcess when appropriate -Discuss: when is this critical (-whatif) and optional (-confirm_ -Discuss: when should you call PSCmdlet.ShouldProcess vs PSCmdlet.ShouldContinue (-Force) +Discuss: when is this critical (-WhatIf) and optional (-Confirm) +Discuss: when should you call $PSCmdlet.ShouldProcess vs $PSCmdlet.ShouldContinue (-Force) ``` -[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact="Medium")] +[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = "Medium")] param([Switch]$Force) -$RejectAll = $false; -$ConfirmAll = $false; +$RejectAll = $false +$ConfirmAll = $false -foreach($file in ls) { +foreach ($file in ls) { - if($PSCmdlet.ShouldProcess( "Removed the file '$($file.Name)'", + if($PSCmdlet.ShouldProcess("Removed the file '$($file.Name)'", "Remove the file '$($file.Name)'?", - "Removing Files" )) { + "Removing Files") + ) { - if($Force -Or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to remove '$($file.Name)'?", "Removing '$($file.Name)'", [ref]$ConfirmAll, [ref]$RejectAll)) { + if ($Force -Or $PSCmdlet.ShouldContinue("Are you REALLY sure you want to remove '$($file.Name)'?", "Removing '$($file.Name)'", [ref]$ConfirmAll, [ref]$RejectAll)) { "Removing $File" @@ -251,48 +234,30 @@ The problems with this have gone away with autoloading, and this is the only way #### Persisting Configuration My choice: Configuration module. Otherwise, use clixml (or XAML) to persist to AppData (TEST: you shouldn't store configuration in your module folder, as it may not survive upgrades (in PowerShell 3 & 4 there was no side-by-side loading)) - #### Provide aliases in your modules You should feel free to create and use aliases within your modules. In some cases, you can even improve readability by using an alias without the verb, or shortening command names. -For exported aliases, follow the guidance of Microsoft ("ip" for import, "s" for set, "g" for get, "r" for remove, etc.), make up somethign for your nouns. +For exported aliases, follow the guidance of Microsoft ("ip" for import, "s" for set, "g" for get, "r" for remove, etc.), make up something for your nouns. Use `New-Alias ... -ErrorAction SilentlyContinue` to avoid overwriting existing aliases. - - - ### GOTCHAS -#### Beware string concatenation with + -You should always wrap this with parentheses, because otherwise it can break (e.g. when passing a string as a parameter. +#### Beware of string concatenation with + +You should always wrap this with parentheses, because otherwise it can break (e.g., when passing a string as a parameter). -#### Beware -match and -like +#### Beware of -match and -like They quietly cast objects to strings (or arrays of strings) -#### Beware -contains and -in -They work on ARRAYS not strings - +#### Beware of -contains and -in +They work on ARRAYS, not strings ### Use Language Features -When writing scripts (as opposed to at the command line), you should almost always choose language features over cmdlets. This includes using if instead of where-object, foreach instead of ForEach-Object, etc. - -The language features are always faster, and almost always more readable. Of course, there are always exceptions, and one exception to this rule is when using foreach will force you to collect a lot of items into an array instead of iterating them as they stream through a pipleine. - - -### You should understand the .Net underpinnings - -#### AVOID appending to arrays in a loop -##### INSTEAD assign output from the loop -#### EXCEPTIONS: -* Appending to multiple collections -* Using Lists instead of arrays +When writing scripts (as opposed to at the command line), you should almost always choose language features over cmdlets. This includes using `if` instead of `Where-Object`, `foreach` instead of `ForEach-Object`, etc. -#### RATIONALE: -* Copying is slow -* Pipeline output uses ArrayList -* Easier to read and understand +The language features are always faster, and almost always more readable. Of course, there are always exceptions, and one exception to this rule is when using `foreach` will force you to collect a lot of items into an array instead of iterating them as they stream through a pipeline. +### You should understand the .NET underpinnings #### AVOID appending to string in a loop ##### INSTEAD assign output from the loop using $OFS @@ -304,22 +269,17 @@ The language features are always faster, and almost always more readable. Of cou * Joining an array of strings is fast * Easier to read and understand +### Strongly typed parameters +Although PowerShell is a dynamic language, we can specify types, and in parameters, it's particularly useful because it hints to users what they can pass to your command. -### Strongly type parameters -Although PowerShell is a dynamic language, we have can specify types, and in Parameters, it's particularly useful because it hints to users what they can pass to your command. - -Strong types on parameters is also crucial because it's a user-input point. Strong types can help you avoid script injection and various other problems with user inputs, and will allow failures to happen as early as possible (even before your command is called). +Strong types on parameters is also crucial because it's a user-input point. Strong types can help you avoid script injection and various other problems with user inputs, and will allow failures to happen as early as possible (even before your command is called). Additionally, avoid using `[string]` with ParameterSets because anything can be cast to it, so PowerShell can't distinguish one parameter set from the other. -When passing on parameters to another command, you should be _at least_ as strongly typed as the other command, to avoid casting exceptions within your script. - -One notable exception is when you could accept more than one type. In PowerShell you can speficy parameter set overloads, but you can't change the type of a parameter. +When passing on parameters to another command, they should be _at least_ as strongly typed as the other command, to avoid casting exceptions within your script. +One notable exception is when you could accept more than one type. In PowerShell you can specify parameter set overloads, but you can't change the type of a parameter. ### Don't reinvent the wheel -### Let's talk about Logging +### Let's talk about logging ### Let's talk about code signing -### Don't reinvent the wheel -### Let's talk about Logging -### Let's talk about code signing \ No newline at end of file diff --git a/Best-Practices/Writing-Parameter-Blocks.md b/Best-Practices/Writing-Parameter-Blocks.md new file mode 100644 index 0000000..4718f6f --- /dev/null +++ b/Best-Practices/Writing-Parameter-Blocks.md @@ -0,0 +1,127 @@ +# Writing Parameter Blocks + +## Always write Help + +For every script and function you should have a comment-based help block (we recommend using a block comment). The best place for these is _inside_ the `function` above the `param` block, but they can also be placed _above_ the function, or at the bottom just before closing. + +In order for it to register as help, you must provide a `.SYNOPSIS` and/or `.DESCRIPTION` + +### Always Write Examples + +You should also always provide at least one example (even if your function doesn't take parameters), where you show the output (or the need to capture it), and explain what happens when the command is run. Note that examples should have the _code_ first, and the _documentation_ of what it does after an empty line or two. + + +```PowerShell +function Test-Help { + <# + .SYNOPSIS + An example function to display how help should be written. + + .EXAMPLE + Test-Help -Name Test-Help + + This tests the help for the Test-Help function. + #> + [CmdletBinding()] + param ( + # This parameter doesn't do anything, but you must provide a value + # Aliases: MP + [Parameter(Mandatory = $true)] + [Alias("MP")] + [String]$MandatoryParameter + ) + + <# code here ... #> +} +``` +### Always Document Every Parameter + +You should always provide at least a brief explanation of each parameter, what its expected or allowed values are, etc. + +The best place for this is a simple comment directly above the parameter (inside the param block) so you don't forget to update it if you remove, rename, or otherwise change the parameter, but you can also place them in the comment help block by using `.PARAMETER ParameterName` and writing the help on the next line. + +## You should specify `[CmdletBinding()]` + +CmdletBinding makes functions and scripts behave like the built-in commands, adding support for the "common" output parameters like `-Verbose` and `-ErrorAction` and supporting `-?` for help. If you don't support it, you risk someone _accidentally_ running your code when they were just trying to `Get-Help` on it. + +There are a few specific advanced cases where you might want to write an old-fashioned script that doesn't use CmdletBinding() -- they are very rare, and are all exceptions to the best practices, so we won't go into them further. + +## You should pick a default ParameterSet + +If you have more than one ParameterSetName on your parameters, you should specify one of them as the `DefaultParameterSetName` in the CmdletBinding. + +## You should support -WhatIf + +If you write a command that changes state, you should probably add `SupportsShouldProcess` to your CmdletBinding. This allows users to specify `-WhatIf` and `-Confirm` when calling your command, so you'll need to actually support those by using `$PSCmdlet.ShouldProcess(...)` or `$PSCmdlet.ShouldContinue(...)` or by passing the preference variable on to other commands you're calling (e.g., `-Whatif:$WhatIfPreference`). + +Here's an example of what that might look like: + +```PowerShell +# NOTE: ConfirmImpact defaults to Medium +# But I recommend setting ConfirmImpact explicitly as a reminder :) +[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Medium")] +param([switch]$Force) + +# You need to pre-define these (because they're passed by [ref]) +$RejectAll = $false +$ConfirmAll = $false + +# Note: please don't actually do this with services, restarting them in non-dependency order would be a nightmare... +foreach ($service in Get-Service | Where Status -eq "Running") { + # This will normally automatically be TRUE. It will only query if the user: + # 1. Has their $ConfirmPreference (default High) set LOWER than or equal to the ConfirmImpact in the cmdlet binding (default Medium) + # 2. Passes -Confirm, which sets the $ConfirmPreference in the function's scope to Low + if ($PSCmdlet.ShouldProcess( "Restarted the service '$($service.Name)'", + "Restart the '$($service.DisplayName)' service ($($service.Name))?", + "Restarting Services" )) { + + # If you use ShouldContinue, have a -Force parameter that bypasses it + # And if you know there may be multiple prompts, you should use this overload that supports the Confirm/Reject "All" option + # In this example, we're only prompting when there are dependent services, and otherwise restart without additional prompting + if ($Force -Or $service.DependentServices.Count -eq 0 -or $PSCmdlet.ShouldContinue( + "$($service.Name) has $($service.DependentServices.Count) dependent services. Are you sure?", + "Restarting the '$($service.DisplayName)' service", + [ref]$ConfirmAll, + [ref]$RejectAll)) { + "(Not actually) restarting $($service.DisplayName)" + } + } +} +``` + +## You should strongly type parameters + +Although PowerShell is a dynamic language, we can specify types, and in parameters, it's particularly useful. + +First, because it hints to users what sort of values they can pass to your command. Is it numeric? Text? An object? + +Second, because using types on parameters helps validate the input, which is crucial because parameters are where you get your user input. Strong types can help you avoid code injection and other problems with user inputs, and will allow failures to happen as early as possible (even before your command is called). + +Additionally, when passing on parameters to another command, they should be _at least_ as strongly typed as the other command, to avoid casting exceptions within your script. + +### Be careful with `[string]` or `[object]` (and `[PSObject]`) + +Obviously [string] is one of the most common parameter types, and [object] is the default type. However, because anything can be cast to these types, you should avoid combining these types with parameters that are designed to differentiate parameter sets, or that accept `ValueFromPipeline` because PowerShell will coerce _everything_ to that. + +Obviously if you want to accept more than one type of object on the same parameter, you have to use `[object]` or `[PSObject]` as the universal base types. You have to be very careful when doing this, and normally should use a `ValidateScript` to ensure the objects are one of your _actual_ supported types. + +### Using [pscredential] + +When you need to accept credentials, you almost always want to name the parameter `$Credential` and accept a [System.Management.Automation.PSCredential](https://docs.microsoft.com/en-us/dotnet/api/System.Management.Automation.PSCredential), which has special support in PowerShell for automatically coercing user names to credentials and more. + +In old versions of PowerShell, you needed to manually decorate these PSCredential parameters with `[System.Management.Automation.CmdletAttribute()]` in order to automatically coerce user names to credentials -- that is, to support the use case where someone writes `-Credential Jaykul` and is automatically prompted for the password using the secure credential prompt. In current versions, this is automatically added when you use the PSCredential type. + +### Using [switch] + +Parameters of type `[switch]` support passing as switches (without a value), and by default cannot be passed by position. + +- Switch parameters should not be given default values. They should always default to false. +- Switch parameters should be designed so that setting them moves a command from its default functionality to a less common or more complicated mode. +- Switch parameters should be treated as boolean values in your scripts. Corrolary: you should not write logic that depends on whether or not the user explicitly passed a value to a switch -- do not attempt to treat a switch as having three states! +- When you need to pass the value of a switch on to another command, you can either splat it, or specify it using the colon syntax for parameters, as in `-TheirSwitch:$MySwitch` + +## Be generous with accept ValueFromPipelineByPropertyName + +For the most flexibility, whenever it's practical, you should write your commands to accept their parameters from the pipeline _by property name_. To enhance your ability to match objects, you can add aliases for the parameter name using the `[Alias()]` attribute. + +Don't forget that values set from the pipeline are only available in the `process` block. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9db3ae4..084a63b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ ## Contributing -Are you interested in helping create the style guide, or do you just want to change a specific rule? You may use the issues system or Github pull requests freely to suggests corrections and simple changes to rules. However, if you want to add a completely new rule, or totally change a rule, please open an issue and let us discuss it! +Are you interested in helping create the style guide, or do you just want to change a specific rule? You may use the issues system or Github pull requests freely to suggest corrections and simple changes to rules. However, if you want to add a completely new rule, or totally change a rule, please open an issue and let us discuss it! ### Tone @@ -20,20 +20,21 @@ The *PowerShell Best Practices* are always evolving, and continue to be edited a To repeat from the ReadMe, the guidelines are divided into these sections: -* Style Guide - * Code Layout and Formatting - * Function Structure - * Documentation and Commenting - * Readability - * Naming Conventions -* Best Practices - * Naming Conventions - * Building Reusable Tools - * Output and Formatting - * Error Handling - * Performance - * Language, Interop and .Net - * Metadata, Versioning, and Packaging +* [Style Guide (Introduction)](Style-Guide/Introduction.md) + * [Code Layout and Formatting](Style-Guide/Code-Layout-and-Formatting.md) + * [Function Structure](Style-Guide/Function-Structure.md) + * [Documentation and Comments](Style-Guide/Documentation-and-Comments.md) + * [Readability](Style-Guide/Readability.md) + * [Naming Conventions](Style-Guide/Naming-Conventions.md) +* [Best Practices (Introduction)](Best-Practices/Introduction.md) + * [Naming Conventions](Best-Practices/Naming-Conventions.md) + * [Building Reusable Tools](Best-Practices/Building-Reusable-Tools.md) + * [Output and Formatting](Best-Practices/Output-and-Formatting.md) + * [Error Handling](Best-Practices/Error-Handling.md) + * [Performance](Best-Practices/Performance.md) + * [Security](Best-Practices/Security.md) + * [Language, Interop, and .NET](Best-Practices/Language-Interop-and-.NET.md) + * [Metadata, Versioning, and Packaging](Best-Practices/Metadata-Versioning-and-Packaging.md) Markdown documents on GitHub support linking within a document, but only to headers, so when editing, in addition to keeping practices and guidelines in the documents where they make sense, please use headlines for each guideline, and lower level headlines for rationale, examples, counter examples, and exceptions. @@ -43,4 +44,6 @@ In general, practices and guidelines should be at least a header with an explana Style guidelines in particular should be phrased as a prescriptive guideline telling people what to do rather than a proscriptive or prohibiting rule, and should have both examples and counter examples. -When you absolutely must write a negative rule, you should start with the phrase "avoid" and end with an "instead" sentence, like:
Avoid the use of `~` to represent the home folder.

The meaning of ~ is unfortunately dependent on the "current" provider at the time of execution. This isn't really a style issue, but it's an important rule for code you intend to share anyway. Instead, use `${Env:UserProfile}` or `(Get-PSProvider FileSystem).Home`

+When you absolutely must write a negative rule, you should start with the phrase "avoid" and end with an "instead" sentence, like: +> ###### Avoid the use of `~` to represent the home folder. +> The meaning of ~ is unfortunately dependent on the "current" provider at the time of execution. This isn't really a style issue, but it's an important rule for code you intend to share anyway. **Instead**, use `${Env:UserProfile}` or `(Get-PSProvider FileSystem).Home` diff --git a/LICENSE.md b/LICENSE.md index 33cfc29..5b474fd 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -8,12 +8,12 @@ This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 Inter **Adapt** — remix, transform, and build upon the material -The authors encourage you to redistribute this content as widely as possible, but require that you give credit to the primary authors below, and that you notify us on github of any improvements you make. +The authors encourage you to redistribute this content as widely as possible, but require that you give credit to the primary authors below, and that you notify us on GitHub of any improvements you make. #### Credits -_The Community Book of PowerShell Practices_ was originally compiled and edited by Don Jones and Matt Penny with input from the Windows PowerShell community on PowerShell.org +_The Community Book of PowerShell Practices_ was originally compiled and edited by Don Jones and Matt Penny with input from the Windows PowerShell community on PowerShell.org. Portions copyright (c) Don Jones, Matt Penny, 2014-2015 @@ -27,8 +27,8 @@ Portions copyright (c) Joel Bennett, 2015 #### NOTE -The *PowerShell Best Practices* are always evolving, and continue to be edited and updated as the language and tools (and our community understanding of them) evolve. We encourage you to check back for new editions at least twice a year, by visiting https://github.com/PoshCode/PowerShellPracticeAndStyle +The *PowerShell Best Practices* are always evolving, and continue to be edited and updated as the language and tools (and our community understanding of them) evolve. We encourage you to check back for new editions at least twice a year, by visiting [https://github.com/PoshCode/PowerShellPracticeAndStyle](https://github.com/PoshCode/PowerShellPracticeAndStyle). -The *PowerShell Style Guide* in particular is in PREVIEW, and we are still actively working out our disagreements about the rules in the guide through the github issues system. Please don't be suprised if over then next few weeks we change rules to contradict what they say at this current moment. +The *PowerShell Style Guide* in particular is in PREVIEW, and we are still actively working out our disagreements about the rules in the guide through the GitHub issues system. Please don't be surprised if over then next few weeks we change rules to contradict what they say at this current moment.

Creative Commons License

diff --git a/README.md b/README.md index fb580bb..e9019bb 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,15 @@

Creative Commons License

-This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License, please attribute to Don Jones, Matt Penny, Carlos Perez, Joel Bennett and the PowerShell Community. +This work is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/), please attribute to Don Jones, Matt Penny, Carlos Perez, Joel Bennett and the PowerShell Community. ###### You are free to: **Share** — copy and redistribute the material in any medium or format -**Adapt** — remix, transform, and build upon the material +**Adapt** — remix, transform, and build upon the material -The authors encourage you to redistribute this content as widely as possible, but require that you give credit to the primary authors below, and that you notify us on github of any improvements you make. +The authors encourage you to redistribute this content as widely as possible, but require that you give credit to the primary authors below, and that you notify us on GitHub of any improvements you make. ### What are Best Practices @@ -30,42 +30,43 @@ Having said that, remember: the points in the Best Practices documents and the S The guidelines are divided into these sections: -* [Style Guide (Introduction)](Style Guide/Introduction.md) - * [Code Layout and Formatting](Style Guide/Code Layout and Formatting.md) - * [Function Structure](Style Guide/Function Structure.md) - * [Documentation and Comments](Style Guide/Documentation and Comments.md) - * [Readability](Style Guide/Readability.md) - * [Naming Conventions](Style Guide/Naming Conventions.md) -* [Best Practices (Introduction)](Best Practices/Introduction.md) - * [Building Reusable Tools](Best Practices/Building Reusable Tools.md) - * [Output and Formatting](Best Practices/Output and Formatting.md) - * [Error Handling](Best Practices/Error Handling.md) - * [Performance](Best Practices/Performance.md) - * [Language, Interop and .Net](Best Practices/Language%2C Interop and .Net.md) - * [Naming Conventions](Best Practices/Naming Conventions.md) - * [Metadata, Versioning, and Packaging](Best Practices/Metadata%2C Versioning%2C and Packaging.md) +* [Style Guide (Introduction)](Style-Guide/Introduction.md) + * [Code Layout and Formatting](Style-Guide/Code-Layout-and-Formatting.md) + * [Function Structure](Style-Guide/Function-Structure.md) + * [Documentation and Comments](Style-Guide/Documentation-and-Comments.md) + * [Readability](Style-Guide/Readability.md) + * [Naming Conventions](Style-Guide/Naming-Conventions.md) +* [Best Practices (Introduction)](Best-Practices/Introduction.md) + * [Naming Conventions](Best-Practices/Naming-Conventions.md) + * [Building Reusable Tools](Best-Practices/Building-Reusable-Tools.md) + * [Output and Formatting](Best-Practices/Output-and-Formatting.md) + * [Error Handling](Best-Practices/Error-Handling.md) + * [Performance](Best-Practices/Performance.md) + * [Security](Best-Practices/Security.md) + * [Language, Interop and .NET](Best-Practices/Language-Interop-and-.NET.md) + * [Metadata, Versioning, and Packaging](Best-Practices/Metadata-Versioning-and-Packaging.md) ### Current State: -Remember [what we mean by _Best Practices_](#what-are-best-practices) +Remember [what we mean by _Best Practices_](#what-are-best-practices). -The *PowerShell Best Practices* are always evolving, and continue to be edited and updated as the language and tools (and our community understanding of them) evolve. We encourage you to check back for new editions at least twice a year, by visiting https://github.com/PoshCode/PowerShellPracticeAndStyle +The *PowerShell Best Practices* are always evolving, and continue to be edited and updated as the language and tools (and our community understanding of them) evolve. We encourage you to check back for new editions at least twice a year, by visiting [https://github.com/PoshCode/PowerShellPracticeAndStyle](https://github.com/PoshCode/PowerShellPracticeAndStyle). -The *PowerShell Style Guide* in particular is in PREVIEW, and we are still actively working out our disagreements about the rules in the guide through the github issues system. +The *PowerShell Style Guide* in particular is in PREVIEW, and we are still actively working out our disagreements about the rules in the guide through the GitHub issues system. #### Contributing Please use the issues system or GitHub pull requests to make corrections, contributions, and other changes to the text - we welcome your contributions! -For more information, see [CONTRIBUTING](CONTRIBUTING.md) +For more information, see [CONTRIBUTING](CONTRIBUTING.md). #### Credits -_The Community Book of PowerShell Practices_ was originally compiled and edited by Don Jones and Matt Penny with input from the Windows PowerShell community on PowerShell.org +_The Community Book of PowerShell Practices_ was originally compiled and edited by Don Jones and Matt Penny with input from the Windows PowerShell community on PowerShell.org. Portions copyright (c) Don Jones, Matt Penny, 2014-2015 -_The PowerShell Style Guide_ was originally created by Carlos Perez, for his students, and all the good parts were written by him. +_The PowerShell Style Guide_ was originally created by Carlos Perez, for his students, and all the good parts were written by him. Portions copyright (c) Carlos Perez, 2015 diff --git a/Style Guide/Code Layout and Formatting.md b/Style Guide/Code Layout and Formatting.md deleted file mode 100644 index cc24a43..0000000 --- a/Style Guide/Code Layout and Formatting.md +++ /dev/null @@ -1,142 +0,0 @@ - - - -### Code Layout & Formatting - -Please note that many of these guidelines, in particular, are purely about readability. Some of them are arbitrary rules, but they are based on decades of traditions in programming, so while you may disagree with some rules (and should always follow the rules of individual projects), when we ask you to leave an empty line after a closing function brace, or two lines before functions, we're not being capricious, we're doing so because it makes it easier for experienced developers to scan your code. - -#### Maintain consistency in layout - -Rules about indentation and line length are about consistency across code bases. Long practice has shown that it's easier to read and understand code when it looks familiar and you're not being distracted by details, which means that (as with the python community), it's better for everyone to follow a single set of rules. - -We aren't trying to force the whole world to change overnight, and the code layout rules for individual projects trump these rules. Whether for legacy reasons, or to match guidelines for multiple languages in a single project, different projects may have different style guides, and since the goal is consistency, you should always abide by the rules in place on your project. - -If you do have a legacy project that is in source control and you decide to reformat code to adopt these rules, try to make all of your whitespace changes in a single a commit that does _nothing_ but edit the whitespace. You should never reformat the whitespace on a file as _part_ of a content change. - - -#### Always write CmdletBinding - -Let's just get this out of the way: all of your scripts should start life as something like this snippet: - -``` -[CmdletBinding()]param() -process{} -end{} -``` - -You can always ignore one of the blocks, and add `begin`, add parameters and so on, but you should never write a script without CmdletBinding, and you should never write one without at least _considering_ making it take pipeline input. - - - -#### Open braces on the same line -Code folding is nicer in many editors. -(TODO: This is in discussion in #24) - -#### Closing braces always on their own line -Because that's how they're supposed to be! -(TODO: This is in discussion in #24) - -#### Prefer: param() begin, process, end -That's the order PowerShell will execute it in -(TODO) - - -#### Indentation - -##### Use four *spaces* per indentation level. - -This is what PowerShell ISE does and understands, and it's the default for most code editors. As always, existing projects may have different standards, but for public code, please stick to 4 spaces, and the rest of us will try to do the same. - -The 4-space rule is optional for continuation lines. Hanging indents (when indenting a wrapped command which was too long) may be indented more than one indentation level, or may even be indented an odd number of spaces to line up with a method call or parameter block. - -```PowerShell - -# This is ok -$MyObj.GetData( - $Param1, - $Param2, - $Param3, - $Param4 - ) - -# This is better -$MyObj.GetData($Param1, - $Param2, - $Param3, - $Param4) -``` - - -#### Maximum Line Length - -Limit lines to 115 characters when possible. - -The PowerShell console is, by default, 120 characters wide, but it allows only 119 characters on output lines, and when entering multi-line text, PowerShell uses a line continuation prompt: `>>> ` and thus limits your line length to 116 anyway. - -Most of us work on widescreen monitors these days, and there is little reason to keep a narrow line width, however, keeping files relatively narrow allows for side-by-side editing, so even narrower guidelines may be established by a given project. Be sure to check when you're working on someone else's project. - -The preferred way to avoid long lines is to use splatting (see [About_Splatting](https://technet.microsoft.com/en-us/library/jj672955.aspx)) and PowerShell's implied line continuation inside parentheses, brackets, and braces -- these should always be used in preference to the backtick for line continuation when applicable, even for strings: - -``` -Write-Host ("This is an incredibly important, and extremely long message. " + - "We cannot afford to leave any part of it out, nor do we want line-breaks in the output. " + - "Using string concatenation let's us use short lines here, and still get a long line in the output") -``` - -#### Blank lines - -Surround function and class definitions with two blank lines. - -Method definitions within a class are surrounded by a single blank line. - -Blank lines may be ommitted between a bunch of related one-liners (e.g. empty functions) - -Additional blank lines may be used sparingly to separate groups of related functions, or within functions to indicate logical sections (e.g. before a block comment). - -End each file with a single blank line. - -#### Trailing spaces - -Lines should not have trailing whitespace. Extra spaces result in future edits where the only change is a space being added or removed, making the analysis of the changes more difficult for no reason. - -#### Spaces around parameters and operators - -You should us a single space around parameter names and operators, including comparison operators and math and assignment operators, even when the spaces are not necessary for PowerShell to correctly parse the code. - -One notable exception is when using semi-colons to pass values to switch parameters: - -```PowerShell -# Do not write: -$variable=Get-Content $FilePath -Wai:($ReadCount-gt0) -First($ReadCount*5) - -# Instead write: -$variable = Get-Content -Path $FilePath -Wait:($ReadCount -gt 0) -TotalCount ($ReadCount * 5) -``` - -#### Spaces around special characters - -White-space is (mostly) irrelevant to PowerShell, but its proper use is the key to writing easily readable code. - -Use a single space after commas and semicolons, and around pairs of curly braces. - -Avoid extra spaces inside parenthesis or square braces. - -Nested expressions `$( ... )` and script blocks `{ ... }` should have a single space _inside_ them to make code stand out and be more readable. - -Nested expressions `$( ... )` and variable delimiters `${...}` inside strings do not need spaces _outside_, since that would become a part of the string. - - -#### Avoid using semicolons (`;`) at the end of each line. - -PowerShell will not complain about extra semicolons, but they are unecessary, and get in the way when code is being edited or copy-pasted. They also result in extra do-nothing edits in source control when someone finally decides to delete them. - -They are also unecessary when declaring hashtables if you are already putting each element on it's own line: - -```PowerShell -# This is the preferred way to declare a hashtable if it must go past one line: -$Options = @{ - Margin = 2 - Padding = 2 - FontSize = 24 -} -``` diff --git a/Style Guide/Naming Conventions.md b/Style Guide/Naming Conventions.md deleted file mode 100644 index c634236..0000000 --- a/Style Guide/Naming Conventions.md +++ /dev/null @@ -1,63 +0,0 @@ -### Naming Conventions - -In general, prefer the use of full explicit names for commands and parameters rather than aliases or short forms. There are tools [Expand-Alias](https://github.com/PoshCode/ModuleBuilder/blob/master/ResolveAlias.psm1) for fixing many, but not all of these issues. - -#### Use the full name of each command. - -Every PowerShell scripter learns the actual command names, but different people learn and use different aliases (e.g.: ls for Linux users, dir for DOS users, gci ...). In your shared scripts you should use the more universally known full command name. As a bonus, sites like GitHub will highlight commands properly when you use the full Verb-Noun name: - -```PowerShell -# Do not write: -gwmi -Class win32_service - -# Instead write: -Get-WmiObject -Class Win32_Service -``` - -#### Use full parameter names. - -Because there are so many commands in PowerShell, it's impossible for every scripter to know every command. Therefore it's useful to be explicit about your parameter names for the sake of readers who may be unfamiliar with the command you're using. This will also help you avoid bugs if a future change to the command alters the parameter sets. - -```PowerShell -# Do not write: -Get-WmiObject win32_service name,state - -# Instead write: -Get-WmiObject -Class win32_service -Property name,state -``` - -#### Use full, explicit paths when possible. - -When writing scripts, it's really only safe to use `..` or `.` in a path if you have previously explicitly set the location (within the script), and even then you should beware of using relative paths when calling .Net methods or legacy/native applications, because they will use the `[Environment]::CurrentDirectory` rather than PowerShell's present working directory (`$PWD`). Because checking for these types of errors is tedious (and because they are easy to over-look) it's best to avoid using relative paths altogether, and instead, base your paths off of $PSScriptRoot (the folder your script is in) when necessary. - -```PowerShell -# Do not write: -Get-Content .\README.md - -# Especially do not write: -[System.IO.File]::ReadAllText(".\README.md") - -# Instead write: -Get-Content (Join-Path $PSScriptRoot README.md) - -# Or even use string concatenation: -[System.IO.File]::ReadAllText("$PSScriptRoot\README.md") -``` - -##### Avoid the use of `~` to represent the home folder. - -The meaning of ~ is unfortunately dependent on the "current" provider at the time of execution. This isn't really a style issue, but it's an important rule for code you intend to share anyway. Instead, use `${Env:UserProfile}` or `(Get-PSProvider FileSystem).Home` ... - -```PowerShell -PS C:\Windows\system32> cd ~ -PS C:\Users\Name> cd HKCU:\Software -PS HKCU:\Software> cd ~ -cd : Home location for this provider is not set. To set the home location, call "(get-psprovider 'Registry').Home = 'path'". -At line:1 char:1 -+ cd ~ -+ ~~~~ - + CategoryInfo : InvalidOperation: (:) [Set-Location], PSInvalidOperationException - + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.SetLocationCommand - -``` - diff --git a/Style-Guide/Code-Layout-and-Formatting.md b/Style-Guide/Code-Layout-and-Formatting.md new file mode 100644 index 0000000..84e5598 --- /dev/null +++ b/Style-Guide/Code-Layout-and-Formatting.md @@ -0,0 +1,251 @@ +### Code Layout & Formatting + +These guidelines are about readability. Some of them are arbitrary rules, but they are based on decades of traditions in programming, so while you may disagree with some rules (and should always follow the rules of individual projects), when we ask you to leave an empty line after a closing function brace, or two lines before functions, we're not being capricious, we're doing so because it makes it easier for experienced developers to scan your code. + +#### Maintain Consistency in Layout + +Rules about indentation, line length, and capitalization are about consistency across code bases. Long practice has shown that it's easier to read and understand code when it looks familiar and you're not being distracted by details, which means that it's better for everyone in the community to follow a single set of rules. + +We don't expect everyone to follow these guidelines, and rules for individual projects always trump these. Whether for legacy reasons, or to match guidelines for multiple languages in a single project, different projects may have different style guidelines. Since the goal is consistency, you should always abide by any style rules that are in place on the project you are contributing to. + +If you do have a legacy project that is in source control and you decide to reformat code to adopt these rules, try to make all of your whitespace changes in a single commit that does _nothing_ but edit the whitespace. You should never reformat the whitespace on a file as _part_ of a content change because it makes the changes hard to spot. + +#### Capitalization Conventions + +PowerShell is **not** case sensitive, but we follow capitalization conventions to make code easy to read. They are based on the [capitalization conventions](https://msdn.microsoft.com/en-us/library/ms229043) Microsoft created for the .NET framework, since PowerShell is a .NET scripting language, and PowerShell cmdlets are primarily written in .NET languages following those guidelines. + +##### Terminology + +* lowercase - all lowercase, no word separation +* UPPERCASE - all capitals, no word separation +* PascalCase - capitalize the first letter of each word +* camelCase - capitalize the first letter of each word _except_ the first. + +PowerShell uses PascalCase for _all_ public identifiers: module names, function or cmdlet names, class, enum, and attribute names, public fields or properties, global variables and constants, etc. In fact, since the _parameters_ to PowerShell commands are actually _properties_ of .NET classes, even parameters use PascalCase rather than camelCase. + +PowerShell language keywords are written in lower case (yes, even `foreach` and `dynamicparam`), as well as operators such as `-eq` and `-match`. The keywords in comment-based help are written in UPPERCASE to make it easy to spot them among the dense prose of documentation. + +```powershell +function Write-Host { + <# + .SYNOPSIS + Writes customized output to a host. + .DESCRIPTION + The Write-Host cmdlet customizes output. You can specify the color of text by using + the ForegroundColor parameter, and you can specify the background color by using the + BackgroundColor parameter. The Separator parameter lets you specify a string to use to + separate displayed objects. The particular result depends on the program that is + hosting Windows PowerShell. + #> + [CmdletBinding()] + param( + [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromRemainingArguments = $true)] + [psobject]$Object, + + [switch]$NoNewline, + + [psobject]$Separator, + + [System.ConsoleColor]$ForegroundColor, + + [System.ConsoleColor]$BackgroundColor + ) + begin { + ... +``` + +As stated previously, PowerShell uses PascalCase for _all_ public identifiers. Function names should follow PowerShell's `Verb-Noun` naming conventions, using PascalCase within both Verb and Noun. + +A special case is made for two-letter acronyms in which both letters are capitalized, as in the variable `$PSBoundParameters` or the command `Get-PSDrive`. Note that ([as specified in the .NET guidelines](https://msdn.microsoft.com/en-us/library/ms229043#Anchor_1)) this does not affect the commonly capitalized (but not acronym) words "OK" and "ID" . You should also not extend it to compound acronyms, such as when Azure's Resource Manager (RM) meets a Virtual Machine (VM) in `Start-AzureRmVM`... + +> We are aware that there are **many** places where these conventions have not been followed properly for various reasons -- you should consider these _exceptions_ (such as for COM interop) or _mistakes_ (such as `System.Data.SqlClient.SQLDebugging`), but not a reason for you to disregard the conventions. + +If you wish, you may use camelCase for variables within your functions (or modules) to distinguish _private_ variables from parameters, but this is a matter of taste. Shared variables should be distinguished by using their scope name, such as `$Script:PSBoundParameters` or `$Global:DebugPreference`. If you are using camelCase for a variable that starts with a two-letter acronym (where both letters are capitalized), both letters should be set to lowercase (such as `adComputer`). + +#### One True Brace Style + +This guide recommends the so-called ["One True Brace Style" variant to K&R](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/81#issuecomment-285835313), which requires that every braceable _statement_ should have the opening brace on the _end of a line_, and the closing brace at the _beginning of a line_. + +There is one notable exception when passing small scriptblocks to parameters (where K&R would allow leaving off the braces entirely), we allow putting the entire statement on a single line. + +```powershell +enum Color { + Black, + White +} + +function Test-Code { + [CmdletBinding()] + param ( + [int]$ParameterOne + ) + end { + if (10 -gt $ParameterOne) { + "Greater" + } else { + "Lesser" + } + } +} + +# An Exception case: +Get-ChildItem | Where-Object { $_.Length -gt 10mb } +``` + +The primary reason for this recommendation is practical: there are no exceptions necessary when following this rule, and when code is written following this style, _new lines_ of code can be inserted between any two lines with no risk of accidentally breaking the code by separating braces from their statement blocks. Thus, it's easier to follow, and makes errors less likely. + +Because this choice was somewhat contentious in the community (about 1/3 of voters opposed), it's worth adding some additional reasoning here. First, in some historical consoles, it was necessary to write this way, so much of the early PowerShell code follows this style anyway. Second, PowerShell functions which accept scriptblocks (such as `ForEach-Object` and `Where-Object`) are common, and an _inherent_ part of the syntax of important PowerShell-based domain-specific languages such as DSC. Since it's **required** to place the opening brace on the end of the line in those cases, the only _consistent_ option is to follow OTBS. + +#### Always Start With CmdletBinding + +All of your scripts or functions should start life as something like this snippet: + +```powershell +[CmdletBinding()] +param () +process { +} +end { +} +``` + +You can always delete or ignore one of the blocks (or add the `begin` block), add parameters and necessary validation and so on, but you should **avoid** writing scripts or functions without `[CmdletBinding()]`, and you should always at least _consider_ making it take pipeline input. + +#### Prefer: param (), begin, process, end + +Having a script written in the order of execution makes the intent clearer. Since there is no functional reason to have these blocks out of order (they _will_ still be executed in the normal order), writing them out of order can be confusing, and makes code more difficult to maintain and debug. + +More explicit code is more maintainable. While PowerShell allows leaving off the explicit name of the `end` block (and even has a `filter` keyword that converts the anonymous block to a `process` block), we recommend against using these features as it results in less explicit code. + +#### Indentation + +##### Use four *spaces* per indentation level + +Usually you will press the `[Tab]` key to indent, but most editors can be configured to insert spaces instead of actual tab characters. For most programming languages and editors (including PowerShell ISE) the default is four spaces, and that's what we recommend. Different teams and projects may have different standards, and when contributing to a project, you should abide by the predominant style, of course. + +```powershell +function Test-Code { + foreach ($exponent in 1..10) { + [Math]::Pow(2, $exponent) + } +} +``` + +Indenting more than 4-spaces is acceptable for continuation lines (when you're wrapping a line which was too long). In such cases you might indent more than one level, or even indent indent an odd number of spaces to line up with a method call or parameter block on the line before. + +```powershell +function Test-Code { + foreach ($base in 1,2,4,8,16) { + foreach ($exponent in 1..10) { + [System.Math]::Pow($base, + $exponent) + } +} +``` + +#### Maximum Line Length + +Limit lines to 115 characters when possible. + +Keeping lines to a small width allows scripts to be read in _one_ direction (top to bottom) without scrolling back-and-forth horizontally. What, exactly, this width should be is a one of the favorite arguing points among developers on the internet (more splintered than emacs vs vi or gnu GPL vs MIT). + +In this guide we use two particular sources for the maximum line width: + +The PowerShell console is, by default, 120 characters wide, but it allows only 119 characters on output lines, and when entering multi-line text, PowerShell uses a line continuation prompt: `>>>` and thus limits your line length to 116 anyway. + +Github's current maximum line width varies between 121 and 126 depending on your browser and OS (and thus, font). However, the 115 line length suggested by PowerShell would be enough to even allow side-by-side diffs to be displayed without scrolling or wrapping on the current "standard" 1080p monitor. + +Again, this is a particularly flexible rule, and you should always follow the guidelines of projects when you're contributing to other people's projects. Although most of us work on widescreen monitors, not everyone can see well without magnification or extremely large fonts. + +The preferred way to avoid long lines is to use splatting (see [Get-Help about_Splatting](https://technet.microsoft.com/en-us/library/jj672955.aspx)) and PowerShell's implied line continuation inside parentheses, brackets, and braces -- these should **always** be used in preference to the backtick for line continuation when applicable, even for strings: + +```powershell +Write-Host -Object ("This is an incredibly important, and extremely long message. " + + "We cannot afford to leave any part of it out, " + + "nor do we want line-breaks in the output. " + + "Using string concatenation lets us use short lines here, " + + "and still get a long line in the output") +``` + +#### Blank Lines and Whitespace + +Surround function and class definitions with _two_ blank lines. + +Method definitions within a class are surrounded by a single blank line. + +Blank lines may be omitted between a bunch of related one-liners (e.g. empty functions) + +Additional blank lines may be used sparingly to separate groups of related functions, or within functions to indicate logical sections (e.g. before a block comment). + +End each file with a single blank line. + +#### Trailing spaces + +Lines should not have trailing whitespace. Extra spaces result in future edits where the only change is a space being added or removed, making the analysis of the changes more difficult for no reason. + +#### Spaces around parameters and operators + +You should use a single space around parameter names and operators, including comparison operators and math and assignment operators, even when the spaces are not necessary for PowerShell to correctly parse the code. + +One notable exception is when using colons to pass values to switch parameters: + +```PowerShell +# Do not write: +$variable=Get-Content $FilePath -Wait:($ReadCount-gt0) -First($ReadCount*5) + +# Instead write: +$variable = Get-Content -Path $FilePath -Wait:($ReadCount -gt 0) -TotalCount ($ReadCount * 5) +``` + +Another exception is when using [Unary Operators](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators#unary-operators): + +```PowerShell +# Do not write: +$yesterdaysDate = (Get-Date).AddDays( - 1) + +$i = 0 +$i ++ + +# Instead write: +$yesterdaysDate = (Get-Date).AddDays(-1) + +$i = 0 +$i++ + +# Same principle should be applied when using a variable. +$yesterdaysDate = (Get-Date).AddDays(-$i) +``` + +#### Spaces around special characters + +White-space is (mostly) irrelevant to PowerShell, but its proper use is key to writing easily readable code. + +Use a single space after commas and semicolons, and around pairs of curly braces. + +Subexpressions `$( ... )` and scriptblocks `{ ... }` should have a single space on the _inside_ of the braces or parentheses to improve readability by making code blocks stand out -- and to further distinguish scriptblocks from variable delimiter braces `${...}` + +Avoid unnecessary spaces inside parenthesis or square braces. + +```powershell +$Var = 1 +"This is a string with one (${Var}) delimited variable." + +"There are $( (Get-ChildItem).Count ) files." +``` + +Obviously, these rules should not be applied in such a way as to affect output. + +#### Avoid Using Semicolons (`;`) as Line Terminators + +PowerShell will not complain about extra semicolons, but they are unnecessary, and can get in the way when code is being edited or copy-pasted. They also result in extra do-nothing edits in source control when someone finally decides to delete them. + +They are also unnecessary when declaring hashtables if you are already putting each element on its own line: + +```PowerShell +# This is the preferred way to declare a hashtable if it extends past one line: +$Options = @{ + Margin = 2 + Padding = 2 + FontSize = 24 +} +``` diff --git a/Style Guide/Documentation and Comments.md b/Style-Guide/Documentation-and-Comments.md similarity index 65% rename from Style Guide/Documentation and Comments.md rename to Style-Guide/Documentation-and-Comments.md index 8122d44..af79fe0 100644 --- a/Style Guide/Documentation and Comments.md +++ b/Style-Guide/Documentation-and-Comments.md @@ -1,8 +1,8 @@ ### Documenting and Comments -Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes! +Comments that contradict the code are worse than no comments. Always make a priority of keeping the comments up-to-date when the code changes! -Comments should be in English, and should be complete sentences. If the comment is short, the period at the end can be ommited. +Comments should be in English, and should be complete sentences. If the comment is short, the period at the end can be omitted. Remember that comments should serve to your reasoning and decision-making, not attempt to explain what a command does. With the exception of regular expressions, well-written PowerShell can be pretty self-explanatory. @@ -14,28 +14,27 @@ $Margin = $Margin + 2 # Maybe write: # The rendering box obscures a couple of pixels. $Margin = $Margin + 2 - ``` #### Block comments Don't go overboard with comments. Unless your code is particularly obscure, don't precede each line with a comment -- doing so breaks up the code and makes it harder to read. Instead, write a single block comment. -Block comments generally apply to some or all of the code which follows them, and are indented to the same level as that code. Each line should start with a # and a single space. +Block comments generally apply to some or all of the code which follows them, and are indented to the same level as that code. Each line should start with a # and a single space. If the block is particularly long (as in the case of documentation text) it is recommended to use the `<# ... #>` block comment syntax, but you should place the comment characters on their own lines, and indent the comment: - + ```PowerShell - # Requiring a space makes things legible and prevents confusion. - # Writing comments one-per line makes them stand out more in the console. +# Requiring a space makes things legible and prevents confusion. +# Writing comments one-per line makes them stand out more in the console. - <# - .Synopsis - Really long comment blocks are tedious to keep commented in single-line mode - .Description +<# + .SYNOPSIS + Really long comment blocks are tedious to keep commented in single-line mode. + .DESCRIPTION Particularly when the comment must be frequently edited, - as with the help and documentation for a function or script - #> + as with the help and documentation for a function or script. +#> ``` #### Inline comments @@ -47,16 +46,16 @@ They should be separated from the code statement by at least two spaces, and ide ```PowerShell $Options = @{ Margin = 2 # The rendering box obscures a couple of pixels. - Padding = 2 # We need space between the border and the text - FontSize = 24 # Keep this above 16 so it's readable in presentations + Padding = 2 # We need space between the border and the text. + FontSize = 24 # Keep this above 16 so it's readable in presentations. } ``` #### Documentation comments -Comment-based help should be written in simple language. +Comment-based help should be written in simple language. -You're not writing a thesis for your college Technical Writing class - you're writing something that describes how a function works. Avoid unecessarily large words, and keep your explanations short. You're not trying to impress anyone, and the only people who will ever read this are just trying to figure out how to use the function. +You're not writing a thesis for your college Technical Writing class - you're writing something that describes how a function works. Avoid unnecessarily large words, and keep your explanations short. You're not trying to impress anyone, and the only people who will ever read this are just trying to figure out how to use the function. If you're writing in what is, for you, a foreign language, simpler words and simpler sentence structures are better, and more likely to make sense to a native reader. @@ -74,34 +73,49 @@ If you want to provide detailed explanations about how your tool works, use the ##### Describe The Function -Every script function command should have at least a short statement describing it's function. That is the `Synopsis`. +Every script function command should have at least a short statement describing its function. That is the `Synopsis`. ##### Document Each Parameter -Each parameter should be documented. To make it easier to keep the comments synchronized with changes to the parameters, the parameter documentation comments may _within_ the `param` block, directly above each parameter. +Each parameter should be documented. To make it easier to keep the comments synchronized with changes to the parameters, the preferred location for parameter documentation comments is _within_ the `param` block, directly above each parameter. +Examples can be found in the ISE snippets: + +```powershell +param ( + # Param1 help description + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0)] + $Param1, + +    # Param2 help description + [int] + $Param2 +) +``` -It is also possible to write `.Parameter` statements with the rest of the documentation comments, but they will be less likely to be left un-updated if you put them closer to the actual code they document. +It is also possible to write `.PARAMETER` statements with the rest of the documentation comments, but experience shows they are more likely to be kept up-to-date if you put them closer to the code they document. ##### Provide Usage Examples -Your help should always provide an example for each major use case. A 'usage example' is just an example of what you would type in to Powershell to run the script - you can even cut and paste one from the command line while you're testing your function. - +Your help should always provide an example for each major use case. A 'usage example' is just an example of what you would type in to PowerShell to run the script - you can even cut and paste one from the command line while you're testing your function. ```PowerShell function Test-Help { <# - .Synopsis - An example function to display how help should be written - .Example + .SYNOPSIS + An example function to display how help should be written. + + .EXAMPLE Get-Help -Name Test-Help - This shows the help for the example function + This shows the help for the example function. #> [CmdletBinding()] - param( + param ( # This parameter doesn't do anything. # Aliases: MP - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [Alias("MP")] [String]$MandatoryParameter ) @@ -110,9 +124,6 @@ function Test-Help { } ``` - - - ### DOC-01 Write comment-based help You should always write comment-based help in your scripts and functions. @@ -120,7 +131,7 @@ You should always write comment-based help in your scripts and functions. Comment-based help is formatted as follows: ```PowerShell -function get-example { +function Get-Example { <# .SYNOPSIS A brief description of the function or script. @@ -129,31 +140,35 @@ function get-example { A longer description. .PARAMETER FirstParameter - Description of each of the parameters + Description of each of the parameters. + Note: +        To make it easier to keep the comments synchronized with changes to the parameters, + the preferred location for parameter documentation comments is not here, + but within the param block, directly above each parameter. .PARAMETER SecondParameter - Description of each of the parameters + Description of each of the parameters. .INPUTS - Description of objects that can be piped to the script + Description of objects that can be piped to the script. .OUTPUTS - Description of objects that are output by the script + Description of objects that are output by the script. .EXAMPLE - Example of how to run the script + Example of how to run the script. .LINK - Links to further documentation + Links to further documentation. .NOTES - Detail on what the script does, if this is needed + Detail on what the script does, if this is needed. #> ``` -Comment-based help is displayed when the user types `help get-example` or `get-example -?`, etc. +Comment-based help is displayed when the user types `help Get-Example` or `Get-Example -?`, etc. Your help should be helpful. That is, if you've written a tool called `Get-LOBAppUser`, don't write help that merely says, "Gets LOB App Users." Duh. -**Further information:** You can get more on the use of comment-based help by typing `help about_Comment_Based_Help` within Powershell. +**Further information:** You can get more on the use of comment-based help by typing `help about_Comment_Based_Help` within PowerShell. diff --git a/Style Guide/Function Structure.md b/Style-Guide/Function-Structure.md similarity index 63% rename from Style Guide/Function Structure.md rename to Style-Guide/Function-Structure.md index 21a955b..046412e 100644 --- a/Style Guide/Function Structure.md +++ b/Style-Guide/Function-Structure.md @@ -3,24 +3,23 @@ Avoid using the `return` keyword in your functions. Just place the object variable on its own. When declaring simple functions leave a space between the function name and the parameters. - + ```PowerShell -function MyFunction ($param1, $param2) -{ - +function MyFunction ($param1, $param2) { + ... } ``` ### Advanced Functions -For Advanced Functions and scripts use the format of **** for +For Advanced Functions and scripts use the format of **\-\** for naming. For a list of approved verbs the cmdlet `Get-Verb` will list them. On the noun side it can be composed of more than one joined word - using Camel Case and only singular nouns. + using Pascal Case and only singular nouns. In Advanced Functions do not use the keyword `return` to return an object. -In Advanced Functions you return objects inside the `Process {}` block +In Advanced Functions you return objects inside the `Process {}` block and not in `Begin {}` or `End {}` since it defeats the advantage of the pipeline. ```PowerShell @@ -29,9 +28,9 @@ function Get-USCitizenCapability { [CmdletBinding()] [OutputType([psobject])] param ( - [Parameter(Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - Position=0)] + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0)] [int16] $Age ) @@ -41,8 +40,7 @@ function Get-USCitizenCapability { DrinkAlcohol = $false Vote = $false } - if ($Age -ge 18) - { + if ($Age -ge 18) { $Capabilities['MilitaryService'] = $true $Capabilities['Vote'] = $true } @@ -56,10 +54,10 @@ function Get-USCitizenCapability { function Get-USCitizenCapability { [CmdletBinding()] [OutputType([psobject])] - param( - [Parameter(Mandatory=$true, - ValueFromPipelineByPropertyName=$true, - Position=0)] + param ( + [Parameter(Mandatory = $true, + ValueFromPipelineByPropertyName = $true, + Position = 0)] [int16] $Age ) @@ -82,144 +80,145 @@ function Get-USCitizenCapability { #### Always use CmdletBinding attribute. -#### Always have at least a `process {}` code block if any parameters takes values from the Pipeline. +#### Always have at least a `process {}` code block if any parameters takes values from the pipeline. + +#### Specify an OutputType attribute if the advanced function returns an object or collection of objects. -#### Specify an OutputType attribute if the advanced function returns - an object or collection of objects. If the function returns different - object types depending on the parameter set provide one per parameter set. +If the function returns different object types depending on the parameter set provide one per parameter set. ```PowerShell -[OutputType([], ParameterSetName="")] -[OutputType("", ParameterSetName="")] +[OutputType([], ParameterSetName = "")] +[OutputType("", ParameterSetName = "")] ``` -#### When a ParameterSetName is used in any of the parameters, always provide a - DefaultParameterSetName in the CmdletBinding attribute. +#### When a ParameterSetName is used in any of the parameters, always provide a DefaultParameterSetName in the CmdletBinding attribute. ```PowerShell function Get-User { - [CmdletBinding(DefaultParameterSetName="ID")] - [OutputType("System.Int32", ParameterSetName="ID")] - [OutputType([String], ParameterSetName="Name")] - param ( - [parameter(Mandatory=$true, ParameterSetName="ID")] + [CmdletBinding(DefaultParameterSetName = "ID")] + [OutputType("System.Int32", ParameterSetName = "ID")] + [OutputType([String], ParameterSetName = "Name")] + param ( + [parameter(Mandatory = $true, ParameterSetName = "ID")] [Int[]] $UserID, - [parameter(Mandatory=$true, ParameterSetName="Name")] + [parameter(Mandatory = $true, ParameterSetName = "Name")] [String[]] $UserName - ) + ) <# function body #> } ``` -#### When using advanced functions or scripts with CmdletBinding attribute avoid validating parameters in the body of the script when possible and use parameter validation attributes instead. +#### When using advanced functions or scripts with CmdletBinding attribute avoid validating parameters in the body of the script when possible and use parameter validation attributes instead. - * **AllowNull** Validation Attribute +* **AllowNull** Validation Attribute The AllowNull attribute allows the value of a mandatory parameter to be null ($null). ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [AllowNull()] [String] $ComputerName - ) + ) ``` - * **AllowEmptyString** Validation Attribute +* **AllowEmptyString** Validation Attribute The AllowEmptyString attribute allows the value of a mandatory parameter to be an empty string (""). ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [AllowEmptyString()] [String] $ComputerName - ) + ) ``` - * **AllowEmptyCollection** Validation Attribute +* **AllowEmptyCollection** Validation Attribute The AllowEmptyCollection attribute allows the value of a mandatory parameter to be an empty collection (@()). ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [AllowEmptyCollection()] [String[]] $ComputerName - ) + ) ``` - * **ValidateCount** Validation Attribute +* **ValidateCount** Validation Attribute The ValidateCount attribute specifies the minimum and maximum number - of parameter values that a parameter accepts. Windows PowerShell + of parameter values that a parameter accepts. PowerShell generates an error if the number of parameter values in the command that - calls the function is outside that range. + calls the function is outside that range. ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateCount(1,5)] [String[]] $ComputerName - ) + ) ``` - * **ValidateLength** Validation Attribute +* **ValidateLength** Validation Attribute - The ValidateLength attribute specifies the minimum and maximum number - of characters in a parameter or variable value. Windows PowerShell generates an + The ValidateLength attribute specifies the minimum and maximum number + of characters in a parameter or variable value. PowerShell generates an error if the length of a value specified for a parameter or a variable is outside of the range. ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateLength(1,10)] [String[]] $ComputerName - ) + ) ``` - * **ValidatePattern** Validation Attribute +* **ValidatePattern** Validation Attribute The ValidatePattern attribute specifies a regular expression that - is compared to the parameter or variable value. Windows PowerShell generates - an error if the value does not match the regular expression - pattern. + is compared to the parameter or variable value. PowerShell generates + an error if the value does not match the regular expression + pattern. + ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidatePattern("[0-9][0-9][0-9][0-9]")] [String[]] $ComputerName - ) + ) ``` - * **ValidateRange** Validation Attribute +* **ValidateRange** Validation Attribute The ValidateRange attribute specifies a numeric range for each - parameter or variable value. Windows PowerShell generates an error - if any value is outside that range. + parameter or variable value. PowerShell generates an error + if any value is outside that range. + ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateRange(0,10)] [Int] $Attempts - ) + ) ``` - * **ValidateScript** Validation Attribute +* **ValidateScript** Validation Attribute The ValidateScript attribute specifies a script that is used - to validate a parameter or variable value. Windows PowerShell + to validate a parameter or variable value. PowerShell pipes the value to the script, and generates an error if the script returns "false" or if the script throws an exception. @@ -233,59 +232,60 @@ function Get-User { [ValidateScript({$_ -ge (get-date)})] [DateTime] $EventDate - ) + ) ``` - * **ValidateSet** Attribute +* **ValidateSet** Attribute - The ValidateSet attribute specifies a set of valid values for a - parameter or variable. Windows PowerShell generates an error if a + The ValidateSet attribute specifies a set of valid values for a + parameter or variable. PowerShell generates an error if a parameter or variable value does not match a value in the set. In the following example, the value of the Detail parameter can only be "Low," "Average," or "High." ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateSet("Low", "Average", "High")] [String[]] $Detail - ) + ) ``` - * **ValidateNotNull** Validation Attribute +* **ValidateNotNull** Validation Attribute The ValidateNotNull attribute specifies that the parameter - value cannot be null ($null). Windows PowerShell generates an - error if the parameter value is null. + value cannot be null ($null). PowerShell generates an + error if the parameter value is null. The ValidateNotNull attribute is designed to be used when the type of the parameter value is not specified or when the specified - type will accept a value of Null. (If you specify a type that will + type will accept a value of null. (If you specify a type that will not accept a null value, such as a string, the null value will be rejected without the ValidateNotNull attribute, because it does not - match the specified type.) + match the specified type.) + ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNull()] $ID - ) + ) ``` - * **ValidateNotNullOrEmpty** Validation Attribute +* **ValidateNotNullOrEmpty** Validation Attribute - The ValidateNotNullOrEmpty attribute specifies that the parameter + The ValidateNotNullOrEmpty attribute specifies that the parameter value cannot be null ($null) and cannot be an empty string (""). - Windows PowerShell generates an error if the parameter is used in + PowerShell generates an error if the parameter is used in a function call, but its value is null, an empty string, or an empty - array. + array. + ```PowerShell param ( - [Parameter(Mandatory=$true)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [String[]] $UserName ) ``` - diff --git a/Style Guide/Introduction.md b/Style-Guide/Introduction.md similarity index 61% rename from Style Guide/Introduction.md rename to Style-Guide/Introduction.md index 0872c53..2839065 100644 --- a/Style Guide/Introduction.md +++ b/Style-Guide/Introduction.md @@ -2,15 +2,14 @@ ## Introduction -In the Python community, developers have a great programming style reference provided as part of the language enhancement process specifications ([PEP-8](https://www.python.org/dev/peps/pep-0008/)), but in the PowerShell world there has been no official documentation of community preferences. +In the Python community, developers have a great programming style reference provided as part of the language enhancement process specifications ([PEP-8](https://www.python.org/dev/peps/pep-0008/)), but in the PowerShell world there has been no official documentation of community preferences. -This document is an attempt to come to an agreement on a style-guide because we know that the more people follow the same set of code-style habits, the more readable the community's code will be. In other words, although the recommendations of this guide are _just recomendations_, if you follow them, you will write PowerShell code that is more easily read, understood, and maintained. +This document is an attempt to come to an agreement on a style-guide because we know that the more people follow the same set of code-style habits, the more readable the community's code will be. In other words, although the recommendations of this guide are _just recommendations_, if you follow them, you will write PowerShell code that is more easily read, understood, and maintained. ## Table of Contents -- [Code Layout and Formatting](Code Layout and Formatting.md) -- [Function Structure](Function Structure.md) -- [Documentation and Commenting](Documentation and Commenting.md) +- [Code Layout and Formatting](Code-Layout-and-Formatting.md) +- [Function Structure](Function-Structure.md) +- [Documentation and Comments](Documentation-and-Comments.md) - [Readability](Readability.md) -- [Naming Conventions](Naming Conventions.md) - +- [Naming Conventions](Naming-Conventions.md) diff --git a/Style-Guide/Naming-Conventions.md b/Style-Guide/Naming-Conventions.md new file mode 100644 index 0000000..0035315 --- /dev/null +++ b/Style-Guide/Naming-Conventions.md @@ -0,0 +1,80 @@ +### Naming Conventions + +In general, prefer the use of full explicit names for commands and parameters rather than aliases or short forms. There are tools like [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer)'s `Invoke-Formatter` and scripts like [Expand-Alias](https://github.com/PoshCode/ModuleBuilder/blob/master/PotentialContribution/ResolveAlias.psm1) for fixing many, but not all of these issues. + +#### Use the full name of each command. + +Every PowerShell scripter learns the actual command names, but different people learn and use different aliases (e.g.: ls for Linux users, dir for DOS users, gci ...). In your shared scripts you should use the more universally known full command name. As a bonus, sites like GitHub will highlight commands properly when you use the full Verb-Noun name: + +```PowerShell +# Do not write: +gps -Name Explorer + +# Instead write: +Get-Process -Name Explorer +``` + +#### Use full parameter names. + +Because there are so many commands in PowerShell, it's impossible for every scripter to know every command. Therefore it's useful to be explicit about your parameter names for the sake of readers who may be unfamiliar with the command you're using. This will also help you avoid bugs if a future change to the command alters the parameter sets. + +```PowerShell +# Do not write: +Get-Process Explorer + +# Instead write: +Get-Process -Name Explorer +``` + +#### Use full, explicit paths when possible. + +When writing scripts, it is only safe to use `..` or `.` in a path if you have previously set the location explicitly (within the current function or script). Even if you _have_ explictly set the path, you must beware of using relative paths when calling .NET methods or legacy/native applications, because they will use `[Environment]::CurrentDirectory` which is not automatically updated to PowerShell's present working directory (`$PWD`). + +Because troubleshooting these types of errors is tedious (and they are easy to over-look), it's best to avoid using relative paths altogether, and instead, base your paths off of $PSScriptRoot (the folder your script is in) when necessary. + +```PowerShell +# Do not write: +Get-Content .\README.md + +# Especially do not write: +[System.IO.File]::ReadAllText(".\README.md") + + +# Although you can write: +Push-Location $PSScriptRoot +Get-Content README.md + +# It would be better to write: +Get-Content -Path (Join-Path -Path $PSScriptRoot -ChildPath README.md) +# Or to use string concatenation: +Get-Content "$PSScriptRoot\README.md" + +# For calling .NET methods, pass full paths: +[System.IO.File]::ReadAllText("$PSScriptRoot\README.md") + +# Optionally by calling Convert-Path +Push-Location $PSScriptRoot +[System.IO.File]::ReadAllText((Convert-Path README.md)) + +``` + +##### Avoid the use of `~` to represent the home folder. + +The meaning of ~ is unfortunately dependent on the "current" provider at the time of execution. This isn't really a style issue, but it's an important rule for code you intend to share anyway. Instead, use `${Env:UserProfile}` or `(Get-PSProvider -PSProvider FileSystem).Home`. + +```PowerShell +PS C:\Windows\system32> cd ~ +PS C:\Users\Name> cd HKCU:\Software +PS HKCU:\Software> cd ~ +cd : Home location for this provider is not set. To set the home location, call "(Get-PSProvider 'Registry').Home = 'path'". +At line:1 char:1 ++ cd ~ ++ ~~~~ + + CategoryInfo : InvalidOperation: (:) [Set-Location], PSInvalidOperationException + + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.SetLocationCommand +``` + + +#### See also the Capitalization Conventions + +In the Code Layout and Formatting chapter, there is a section on [capitalization conventions](Code-Layout-and-Formatting.md#Capitalization-Conventions). diff --git a/Style Guide/Readability.md b/Style-Guide/Readability.md similarity index 91% rename from Style Guide/Readability.md rename to Style-Guide/Readability.md index cb6b6af..16605f2 100644 --- a/Style Guide/Readability.md +++ b/Style-Guide/Readability.md @@ -1,4 +1,4 @@ -TODO: This section should probably be merged to [Code Layout and Formatting](Code Layout and Formatting.md), and based on the #15, we should remove or rewrite the backticks section. +TODO: This section should probably be merged to [Code Layout and Formatting](Code-Layout-and-Formatting.md), and based on the [#15](https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/15), we should remove or rewrite the backticks section. # READ-01 Indent your code @@ -26,7 +26,7 @@ Continuing in that vein, understand that the following are basically guidelines First, format your code properly. The convention is to indent within constructs, to make it clearer what "belongs to" the construct. ```PowerShell -ForEach ($computer in $computers) { +foreach ($computer in $computers) { Do-This Get-Those } @@ -34,7 +34,6 @@ ForEach ($computer in $computers) { You will probably be reviled if you don't format carefully. - # READ-02 Avoid backticks Consider this: diff --git a/Style Guide/TODO.md b/Style-Guide/TODO.md similarity index 83% rename from Style Guide/TODO.md rename to Style-Guide/TODO.md index 9b15aae..ed858bd 100644 --- a/Style Guide/TODO.md +++ b/Style-Guide/TODO.md @@ -1,14 +1,7 @@ These documents are in an extremely rough state, not suitable for inclusion in the main guide yet. - - - [Include Help](#include-help) - - - - - ### Include Help Discuss: Add a blank line between comment based help and function declaration? diff --git a/TableOfContents.md b/TableOfContents.md index ad0cd11..2d89708 100644 --- a/TableOfContents.md +++ b/TableOfContents.md @@ -1,19 +1,27 @@ PowerShell Practice and Style Guide =================================== -[Introduction (ReadMe)](ReadMe.md) +## Introduction +* [About this Guide](README.md) +* [Contributing](CONTRIBUTING.md) -* [Style Guide (Introduction)](Style Guide/Introduction.md) - * [Code Layout and Formatting](Style Guide/Code Layout and Formatting.md) - * [Function Structure](Style Guide/Function Structure.md) - * [Documentation and Comments](Style Guide/Documentation and Comments.md) - * [Readability](Style Guide/Readability.md) - * [Naming Conventions](Style Guide/Naming Conventions.md) -* [Best Practices (Introduction)](Best Practices/Introduction.md) - * [Building Reusable Tools](Best Practices/Building Reusable Tools.md) - * [Output and Formatting](Best Practices/Output and Formatting.md) - * [Error Handling](Best Practices/Error Handling.md) - * [Performance](Best Practices/Performance.md) - * [Language, Interop and .Net](Best Practices/Language%2C Interop and .Net.md) - * [Naming Conventions](Best Practices/Naming Conventions.md) - * [Metadata, Versioning, and Packaging](Best Practices/Metadata%2C Versioning%2C and Packaging.md) \ No newline at end of file +## Style Guide + +* [Introduction](Style-Guide/Introduction.md) +* [Code Layout and Formatting](Style-Guide/Code-Layout-and-Formatting.md) +* [Function Structure](Style-Guide/Function-Structure.md) +* [Documentation and Comments](Style-Guide/Documentation-and-Comments.md) +* [Readability](Style-Guide/Readability.md) +* [Naming Conventions](Style-Guide/Naming-Conventions.md) + +## Best Practices + +* [Introduction](Best-Practices/Introduction.md) +* [Naming Conventions](Best-Practices/Naming-Conventions.md) +* [Building Reusable Tools](Best-Practices/Building-Reusable-Tools.md) +* [Output and Formatting](Best-Practices/Output-and-Formatting.md) +* [Error Handling](Best-Practices/Error-Handling.md) +* [Performance](Best-Practices/Performance.md) +* [Security](Best-Practices/Security.md) +* [Language, Interop and .NET](Best-Practices/Language-Interop-and-.NET.md) +* [Metadata, Versioning, and Packaging](Best-Practices/Metadata-Versioning-and-Packaging.md) diff --git a/book.json b/book.json new file mode 100644 index 0000000..5610cef --- /dev/null +++ b/book.json @@ -0,0 +1,5 @@ +{ + "structure": { + "summary": "TableOfContents.md" + } +}