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
Mark Jacob, Cisco Instructor, presents an introduction to Cisco Modeling Labs 2.0 or CML2.0, an upgrade to Cisco’s VIRL Personal Edition. Mark demonstrates Terminal Emulator access to console, as well as console access from within the CML2.0 product. Hello, I’m Mark Jacob, a Cisco Instructor and Network Instructor at Interface Technical Training. I’ve been using … Continue reading A Simple Introduction to Cisco CML2
This content is from our CompTIA Network + Video Certification Training Course. Start training today! In this video, CompTIA Network + instructor Rick Trader demonstrates how to use cable testers in network environments. Let’s look at some tools that we can use to test our different cables in our environment. Cable Testers Properly Wired Connectivity … Continue reading Cable Testers and How to Use them in Network Environments
In this video, PowerShell instructor Jason Yoder shows how to add Methods (PSMethod) to your code using free software that’s added into the PSObject. For instructor-led PowerShell courses, see our course schedule. Microsoft Windows PowerShell Training Download the Building Methods PowerShell script</a> used in this video. <# ╔══════════════════════════════════════════════════════════════════════════════╗ ║ ║ ║ Building Methods ║ ╟──────────────────────────────────────────────────────────────────────────────╢ … Continue reading How to Build in a PSMethod to your PowerShell Code