A working example tells more than a thousand words, so here you’ll find a growing repository of working PowerShell example code. In my projects and consultancy, I come across new challenges almost daily. I have started to organize and publish the findings here so you can benefit from our work as well. Simply click on one of the subjects in the menu bar on the left. On mobile devices, the navigation menu may be available via a separate menu button.
I am only starting this repo. So when I am talking about “sections” and a plethora of sample scripts, this is not exactly true right now. Come back often to see this section grow!
Here are a couple of notes on this website, and how you best view and copy the code examples from the webpages into your PowerShell editor of choice.
All code samples are displayed without word wrap to minimize confusion about where a code line ends. When code does not fit, we do not show scrollbars unless you hover over the code example for a good reading experience. When you hover over code examples, a horizontal scrollbar shows.
On mobile devices, to scroll example code, tap anywhere into the example code box, then slide the code to the right or left.
Running Example Code
First and foremost: understand any code before you run it! While we do not publish dangerous code, there may occasionally be risk attached, i.e. when code illustrates how to change system settings.
Please avoid running code examples out of context: in order to minimize redundant code and focus on the relevant things, our articles typically explain features or strategies step by step. A given code example may require that you ran other code from a previous example before, i.e. to define a function or extension.
For complex code examples, our articles typically show a complete example at the beginning or the end, or reference to PowerShell modules where you can load the entire code.
To run example code, click the icon in the upper right corner of each code sample box. This automatically selects all code in your browser and copies it to the clipboard. From there, paste it into the editor of choice, i.e. ISE or VSCode.
While you can paste code into PowerShell console windows, this is not recommended for larger portions of code as PowerShell accepts multi-line code line-by-line and may start executing lines while others are still pasted.
All of our articles are licensed under a Attribution-NoDerivatives 4.0 International license, so you cannot publish them (or parts) in magazines, your website, or any other place without our written permission. Unfortunately, in the past we have seen websites and magazines harvest articles and tricks, and publish them under their name (which is obviously violating community spirit).
Code Is Public Domain
All code is licensed under a very permissive Attribution 4.0 International license, so you can use any code freely, even in commercial projects. You can use the code as-is or use it as a start for your own scripts. Obviously we would love to be mentioned if you do but that is no requirement.
All code samples are published as-is without warranty of any kind. Use code exclusively at your own risk and discretion.
To give in return, we kindly ask that you leave a comment below
- when you spotted a flaw
- have a better idea or approach to the problem
- or would like to contribute your very own code examples.
If you would like to share your own code samples, before you submit them via the comment system below, please quickly review your sample code and make sure:
- it is short and concise: just a couple of lines of code please, focusing on one essential thing. If you have large scripts, you may want to extract the beef and turn it into one or more simple code samples.
- it is tested: please run your sample and make sure it works as expected. Keep an eye on dependencies. If your code sample requires prerequisites such as other modules, or if it is running only in certain environments, please add a comment section explaining the prerequisites that need to be met to run the code.
- it is polished: please take a minute and prettify your code. Use “speaking” variable names, and try and not use aliases. We’d all like to learn from code, so it should adhere to the common best practices.
- it does not contain secrets: Make sure your sample code is anonymized and does not contain internal urls or server names, let alone usernames or passwords.