We help IT Professionals succeed at work.

PowerShell CMDLETS vs. Scripts Vs. Functions

compdigit44 used Ask the Experts™
I'm slowly trying to learn POwershell and all of the experts have been a great help in my learning process..

I can;t understand the following though..

What is the difference betweeen a powershell: script, cmdlet, function or external / internal  command
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'll try make these as simple as possible :)

A script is a collection of commands which are executed. For example, you may have a script which gather a list of running processes on your PC or another PC, and then it may have a function which does something with that data or you might also run some other commands. So think of it as a sequence of commands being run without having to run the commands 1 by 1 yourself.

A cmdlet is .NET code which performs a specific task without you having to write it out yourself. For example instead of just typing Get-Process and using it's nifty switches, you could use the below code and work with it yourself to get the various output you want.


A function lets you do a repeatable task or a whole bunch of tasks without having to type it out over and over (think of a function as maybe a small script? :p). So rather than have 10/1, 10/2, 10/5, etc you could have a function which you fed the numbers to divide 10 by do you don't have to type it out over and over.

Internal commands
These are cmdlets.

External commands
Are other programs that exist outside of PowerShell. For example ping.exe.

In my opinion:
Function: series of commands saved under a name in a PowerShell session that can be called. Functions can have parameters.

Script: similar to functions, but it is saved as a file (with an extension of PS1), so this "function" is preserved among PowerShell sessions, and they are referenced  by the file name. Scripts can also have parameters.

Cmdlet: a code that follows very strict rules for its behaviour, such as its name is constructed as a verb-noun pair.

There is a connection between cmdlets and script/funtions in PowerShell v2, the so called "advanced functions/script cmdlets", where you can write functions or scripts that behaves very much like the cmdlets. They can require confirmation, use the "-whatif" switch, can have dynamic parameters (parameters that exist only on specific PSDrives), can consume parameters from the pipeline, can have parameter validations, etc.


that's for the great replies..

I'm still having a hard time understanding the difference between scripts and functions in Powershell..

Also, if one was to create a new cmdlet how to you know what .net codes to use? How would you build a cmdlet????? This  is a mistry to me
PowerShell Developer
Top Expert 2010

Practically speaking there's not much of a difference. A function must be present in the session if you're to use it, a script is saved to a file.

This is a very simple function:

Function Hello-World {
  Return "Hello World"

You can run that one with Hello-World once you've pasted it into the prompt.

This is an advanced function which implements help and binds as a CmdLet (it will not be easy to distinguish from a CmdLet):

Function Say-Hello {
      A brief summary of the function
      A full description of the function
    .Parameter Name
      The name parameter

    [String]$Name = "everyone"

  Return "Hello $Name"

The first section, enclosed with <# #>, it's a comment block. The .Synopsis, .Parameter and .Description are (a few of the) keywords used to write help. [CmdLetBinding()] is required or it won't let you use Help (although it won't complain if you forget it); CmdLetBinding can also be used to define how a CmdLet / Function should work.

Copy and paste Say-Hello into the PowerShell prompt if you want to see how it working. You'll see that Get-Help Say-Hello -Full" shows the details entered above. You can use the CmdLet simply by typing "Say-Hello", or "Say-Hello compdigit44". Close down PowerShell and the function is gone, you'd have to paste it into the prompt again.

If you want it to stick around, you might drop the Function line and the trailing } and save that as a .ps1 file. Now you have a script instead of a function. It'll still support Get-Help, this time using the file name.

Another step back, you might decide you have a collection of functions that you use a lot, and you might decide to save those as a script Module (a .psm1 file). I have a few of those, one of which stores all my network functions, including these:


If you step away from PowerShell and want to start writing complex CmdLets then C# or VB .NET are available. You can compile your CmdLet into a DLL file and import it as a Module. Exactly how you do that depends on what you'd like your CmdLet to do.

If you want to explore that further then MSDN should be somewhat near the top of places to visit. However, it helps to have a grounding in PowerShell, and .NET, before moving in that direction.