What is & in PowerShell

What is & in PowerShell

Q. What is & in PowerShell?

A. & is the call operator that will execute code. Normally we just execute cmdlets as their name is in the search path however consider you save commands in a string and want those commands to execute. The call operator will do that. For example:

PS C:\> $commandstorun = "get-host"

PS C:\> $commandstorun
get-host

PS C:\> & $commandstorun


Name : Windows PowerShell ISE Host
Version : 5.1.14393.693
InstanceId : 15332c57-1c14-4752-a66c-c95bcb99fb97
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-US
CurrentUICulture : en-US
PrivateData : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled : True
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish