Answer the question
In order to leave comments, you need to log in
How to create a cmdlet in PowerShell?
Hello, I have a task:
Answer the question
In order to leave comments, you need to log in
Alternatively, you can do this.
Create file test.psm1 :
Function Get-NProcesses
{
param (
[switch]$Multiple,
[switch]$LastProcess
)
$listProcesses = Get-Process| Select-Object -Unique | Select-Object -Property ProcessName, StartTime | Where-Object { $_.StartTime -ne $null }
if($Multiple)
{
foreach($process in $listProcesses.ProcessName)
{
if((Get-Process $process).Count -gt 1)
{
$process
}
}
}
if($LastProcess)
{
$listProcesses | Sort-Object -Property StartTime | Select-Object -Last 1
}
}
Import-Module /path/to/test1.psm1
Get-NProcesses -Multiple
Get-NProcesses -LastProcess
A cmdlet is a PowerShell file with a set of functions. You need to format these pipelines as PS functions and save them to a file with the psm extension, in one of the folders:
C:\Users\%UserName%\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\Windows \System32\WindowsPowerShell\v1.0\Modules
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question