How should I name my PowerShell Scripts?
How should I name my PowerShell Scripts?
I get this question a lot, and just recently on the Windows PowerShell Forum on Technet.
Now let’s talk about this for a minute. If you're one of those people wondering what the answer is, you will see many answers that may confuse you further. The most common response is “put your stuff into a module”.
Modules are scripts that contain Advanced Functions. Basically, when someone tells you to make a “module”, they are saying to build all of your scripts into cmdlets using Advanced Functions, then store them in a special script that has a .psm1 extension.
This is a good answer because now your cmdlets are very portable (just copy the module) and easy to import and use.
The bad part of this answer is you have to know what an Advanced Function is, how to make modules, and how to write your script into an Advanced Function. It’s not easy if you're just starting out with PowerShell.
To be honest, I’m a big fan of Advanced Functions and Modules, but not as a solution to this problem.
Scripts Are Good
Many people starting out with PowerShell are not “Tool Makers” that would make modules. Many of them are writing small scripts to get things done. I write scripts all the time that should not be turned into cmdlets. So, If you're like me or just getting started with scripting in PowerShell, what do you do if you don’t want to make a module, but still need to organize your scripts?
Simple. Use PowerShell to help you.
How to name your PowerShell Scripts
All PowerShell cmdlets are named with a verb-noun. When you use Get-Command, it lists them alphabetically.
Over time, you get used to seeing everything as a verb-noun listed alphabetically. So don’t fight it. Name your scripts the same way.
The super guru MVP of PowerShell Don Jones just recently wrote about this. He is correct. So, here is how I name my scripts.
1. Use the same verbs that Microsoft uses for the cmdlets. The reason is simple, you need to be consistent so everyone understands what the script will do. At a PowerShell prompt, type Get-Verb and choose the best verb for what the script does. While there are many choices, you’re best to stay in the “Common” ones if possible.
PowerShell Get Verb for Script
2. Choose the noun. This is totally up to you, but the idea is to specify “what” will be affected. As an additional note, the cool people only use the singular of the noun. It’s not “Get-Services”, it’s “Get-Service”.
3. Put a dash in between the verb and the noun! Yup, just like the cmdlet’s.
4. Put all of them into a folder. Don’t go nuts here and start to create a bunch of sub folders. I find that Admin’s are anal-as-hell and want to over organize everything. The point to naming the scripts like a cmdlet is that they will automatically organize themselves and provide a similar view like Get-Command.
5. Don’t believe me, try it!
Here is a Windows Explorer view of a sample directory.
Here is a PowerShell view.
Isn’t that much easier to organize your scripts? After years of working with Admins, I find this keeps things simple and easy to find. It’s also very portable and easy to copy. DON’T COMPLICATE THIS!
The beauty of how the PowerShell Team designed the cmdlet naming can be put to use with your script naming as well. Don’t look for “Graphical Script Repositories” and the such. Use what you already know and keep the solution simple for everyone!
Knowledge is PowerShell
You May Also Like
Video transcription Steve Fullmer: In our Windows training courses, we often share information about the Windows 8.1 Mobility Center. Mobility Center was introduced for mobile and laptop devices in Windows 7. It’s present and somewhat enhanced in Windows 8. Since we don’t have mobile devices in our classrooms, I decided to take a little bit … Continue reading Configuring Windows Mobility Center and How to Turn it On and Off
In this video, Cisco CCNA & CCNP instructor Mark Jacob shows how to troubleshoot OSPF Adjacency issues by showing the distance between routers with the show ip ospf neighbor command.
In this video, you will gain an understanding of Agile and Scrum Master Certification terminologies and concepts to help you make better decisions in your Project Management capabilities. Whether you’re a developer looking to obtain an Agile or Scrum Master Certification, or you’re a Project Manager/Product Owner who is attempting to get your product or … Continue reading Agile Methodology in Project Management