Questions tagged [powershell-cmdlet]

Quote from Microsoft site: "A cmdlet is a lightweight command that is used in the PowerShell environment. The PowerShell runtime invokes these cmdlets within the context of automation scripts that are provided at the command line. The PowerShell runtime also invokes them programmatically through PowerShell APIs"

Ref: Cmdlet Overview

882 questions
260
votes
20 answers

How to get the current directory of the cmdlet being executed

This should be a simple task, but I have seen several attempts on how to get the path to the directory where the executed cmdlet is located with mixed success. For instance, when I execute C:\temp\myscripts\mycmdlet.ps1 which has a settings file at…
Stig Perez
  • 3,595
  • 4
  • 23
  • 36
65
votes
3 answers

What is [cmdletbinding()] and how does it work?

According to get-help about_Functions_CmdletBindingAttribute The CmdletBinding attribute is an attribute of functions that makes them operate like compiled cmdlets We can use it on the top of our scripts. What is the function in this case? An…
52
votes
9 answers

How to properly use the -verbose and -debug parameters in a custom cmdlet

By default, any named function that has the [CmdletBinding()] attribute accepts the -debug and -verbose (and a few others) parameters and has the predefined $debug and $verbose variables. I'm trying to figure out how to pass them on to other…
Micah
  • 111,873
  • 86
  • 233
  • 325
45
votes
1 answer

How do I deal with Paths when writing a PowerShell Cmdlet?

What is the proper way to receive a file as a parameter when writing a C# cmdlet? So far I just have a property LiteralPath (aligning with their parameter naming convention) that is a string. This is a problem because you just get whatever is typed…
Matthew
  • 11,203
  • 9
  • 38
  • 44
42
votes
10 answers

Can we see the source code for PowerShell cmdlets?

I'm learning some PowerShell. Is it possible to see the source code for a built-in cmdlet like Get-ChildItem?
Blankman
35
votes
2 answers

What is the difference between a cmdlet and a function?

There are two elements in a module manifest: cmdlet and function. What is the difference between a cmdlet and a function?
mazzy
  • 1,025
  • 1
  • 13
  • 26
30
votes
3 answers

How to save each line of text file as array through powershell

If I have a text file, C:\USER\Documents\Collections\collection.txt that has the following information: collectionA.json collectionB.json collectionC.json collectionD.json I am wondering how, through Powershell, I am able to store each line in the…
JOberloh
  • 1,026
  • 2
  • 11
  • 20
30
votes
8 answers

How do I find the Azure PowerShell version?

I need to find the installed Azure PowerShell version through cmdlets code. How do I find the Azure PowerShell version? Note: Other than cmdlets code is also welcome.
ManirajSS
  • 2,295
  • 5
  • 26
  • 50
28
votes
5 answers

How do you support PowerShell's -WhatIf & -Confirm parameters in a Cmdlet that calls other Cmdlets?

I have a PowerShell script cmdlet that supports the -WhatIf & -Confirm parameters. It does this by calling the $PSCmdlet.ShouldProcess() method before performing the change. This works as expected. The problem I have is that my Cmdlet is…
Dan Finucane
  • 1,547
  • 2
  • 18
  • 27
24
votes
4 answers

Use PowerShell to stop-process, can i bypass confirm?

I'm a powershell newbie, but I often find myself starting and stopping a small group of services when I'm debugging some code. In Powershell I can easily stop the processes using a wildcard but it makes me confirm. There is a -confirm parameter, but…
Hcabnettek
  • 12,678
  • 38
  • 124
  • 190
21
votes
6 answers

MSOnline can't be imported on PowerShell (Connect-MsolService error)

I had this issue and couldn´t find any answer. The issue was that I was trying to use Azure cdmlets to connect to O365 via c# code, but I couldn´t get the connect-msolservice. ""The term is not recognized" error when you try to run administrative…
Danny Fallas
  • 628
  • 1
  • 5
  • 11
19
votes
1 answer

How do I host a Powershell script or app so it's accessible via WSManConnectionInfo? (like Office 365)

The only ways I know to connect to a remote runspace include the following parameters WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "localhost", 80, "/Powershell",…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
19
votes
1 answer

Is it possible to hide the user input from read-host in Powershell?

I´m looking for a way to hide the user input from the Read-Host cmdlet. I know I can do this with -assecurestring, but I´d like to save the input as plain text in my variable. Is there a possible way to do this?
Tobias
  • 193
  • 1
  • 1
  • 5
19
votes
5 answers

Export Powershell 5 enum declaration from a Module

I have an enum type defined within a module. How do I export it to be accessible from outside once the module has been loaded? enum fruits { apple pie } function new-fruit { Param( [fruits]$myfruit ) write-host…
19
votes
3 answers

stop-service cmdlet timeout possible?

We're using the stop-service cmdlet to kill a few services on our boxes. Most of the time it works great, however we have one or two services (who doesn't?) that occasionally don't play nice. In this instance one of the services in question will…
larryq
  • 15,713
  • 38
  • 121
  • 190
1
2 3
58 59