PowerShell how to get reference to function or cmdlet?
I would like to get a link to a function or cmdlet.
For example, I would like to reference the Get-ChildItem cmdlet. I don't want to call this, I want a function reference so that I can then navigate to another function.
Is there any syntax for this?
I know that I can call the expression with the string "Get-ChildItem". Is this the only way to handle this situation?
source to share
$gci = Get-Command Get-ChildItem
This will do it .. but since you didn't say how you want to use it, I'm not sure if this is what you want.
Edit: After viewing the comment, it seems you want to store it in a variable. You can do it with script functions, but I don't think you could do it with the contents of the cmdlet like this, unless you wrap it in the scriptblock.
source to share
Invoke-Expression is a tool designed to handle just this use case. Note that if you need to define some variables in a string when you execute it, you must avoid that variable with the serious `sign, or you can instead define a single-quote string here instead.
$commands = @"
`$computername = `$env:computername
get-wmiobject win32_computersystem -computername `$computername
"@
Invoke-Expression $commands
>Domain : FOXDEPLOY
Manufacturer : Dell Inc.
Model : Latitude E6540
Name : DELLBOOK
PrimaryOwnerName : Windows User
TotalPhysicalMemory : 17080483840
source to share