Welcome to MSDN Blogs Sign in | Join | Help

Fun with Script Cmdlets

 

Script Cmdlets are one of the coolest things about the newer version of PowerShell.  A Script cmdlet allows you to use all of the variety of cmdlet parameter sets inside of PowerShell functions.

Since Script Cmdlets are PowerShell functions, and the PowerShell engine prefers to run functions rather than commands, you can use Script Cmdlets to override an existing cmdlet.  You might want to do this to add or remove parameters from a cmdlet you use often.  Also, you might just want to see what a cmdlet’s parameters look like in a script cmdlet, so you can go write your own.

Luckily, Powershell  has a way to generate script cmdlets from an existing cmdlet.  Below is a script cmdlet, called New-ScriptCmdlet, that I’ll use to create other script cmdlets.

You can use this to create new script cmdlets from existing cmdlets in a couple of different ways:

# Create a new PowerShell cmdlet from an existing cmdlet

Get-Command Get-Command | New-ScriptCmdlet Get-Command  | Set-Content Get-Command.ps1

# Create a new PowerShell cmdlet from an existing cmdlet’s type

[Microsoft.PowerShell.Commands.GetProcessCommand] | New-ScriptCmdlet Get-Process | Set-Content Get-Process.ps1

# Creates a new PowerShell cmdlet from a random existing command and puts it into a file of the same name

 Get-Command | Get-Random | Foreach-Object {
    $cmdlet = $_
    $scriptCmdlet  = $cmdlet | New-ScriptCmdlet $cmdlet.Name
    $scriptCmdlet | Set-Content "$($cmdlet.Name).ps1"
}

Hope this helps,

James Brundage [MSFT]

function New-ScriptCmdlet()

{

cmdlet `

 -DefaultParameterSet Type

    param(

    [Parameter(ParameterSetName="Type",ValueFromPipeline=$true,Position=1)]

    [Type]

    $type,

   

    [Parameter(ParameterSetName="CommandInfo",ValueFromPipeline=$true,Position=1)]

    [Management.Automation.CmdletInfo]

    $commandInfo,

 

    [Parameter(Position=0)]

    [string]

    $name

    )

 

    Process

    {

        if (! $type) {

            if ($commandInfo.ImplementingType) { $type = $commandInfo.ImplementingType }

        }

 

        if ((! $type) -and (! $commandInfo)) {

@"

$(if ($name) { 'function ' + $name + '() {' })

cmdlet `

param()

begin {}

process {}

end {}

$(if ($name) {'}' })

"@              

        } else {

            if (! ($type.IsSubclassOf([Management.Automation.Cmdlet]))) {

                throw "Must provide a cmdlet to create a proxy"           

            }

            $commandMetaData = New-Object Management.Automation.CommandMetadata $type

            $proxyCommand =                                

@"

$(if ($name) { 'function ' + $name + '() {' })

$([Management.Automation.ProxyCommand]::Create($commandMetaData))

$(if ($name) {'}' })

"@

            $executionContext.InvokeCommand.NewScriptBlock($proxyCommand)                         

        }

    }

}

Published Friday, May 09, 2008 12:42 AM by PowerShellTeam

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Fun with Script Cmdlets

Is that possible to create a script that works as script cmdlet? Help topic about_ScriptCmdlets claims: “In contrast, When an unnamed script cmdlet is written within a script file, running the script file does invoke the cmdlet.”. I cannot make it working (unnamed script cmdlet in a script), examples look not updated for CTP2 syntax.

Friday, May 09, 2008 8:24 AM by Roman Kuzmin

# Dew Drop - May 9, 2008 | Alvin Ashcraft's Morning Dew

# re: Fun with Script Cmdlets

The script cmdlet syntax changed from 1st CTP to 2nd CTP.  Personally, I find that I often use the tricks above to generate a script cmdlet and then I store it into a file for later use. If you omit the Name parameter, it will produce an anonymous script cmdlet that you can just run.

Looking over the help, I think it's likely that it did not get updated for the 2nd CTP.  I will inform our tech writers.

Friday, May 09, 2008 2:39 PM by PowerShellTeam

# re: Fun with Script Cmdlets

I thought you were moving away from calling them script cmdlets ?

Friday, May 09, 2008 5:47 PM by karl prosser

# Extending Active Directory Powershell

Today, I am going to show how we can leverage the power of Powershell V2 and Active Directory Module,

Friday, March 20, 2009 3:00 AM by Active Directory Powershell Blog

# Extending Active Directory Powershell

Today, I am going to show how we can leverage the power of Powershell V2 and Active Directory Module,

Friday, March 20, 2009 3:19 AM by Active Directory Powershell Blog

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker