How do I include a locally defined function when using PowerShell’s Invoke-Command for remoting?

You need to pass the function itself (not a call to the function in the ScriptBlock). I had the same need just last week and found this SO discussion So your code will become: Invoke-Command -ScriptBlock ${function:foo} -argumentlist “Bye!” -ComputerName someserver.example.com -Credential someuser@example.com Note that by using this method, you can only pass parameters into … Read more

Using PowerShell credentials without being prompted for a password

The problem with Get-Credential is that it will always prompt for a password. There is a way around this however but it involves storing the password as a secure string on the filesystem. The following article explains how this works: Using PSCredentials without a prompt In summary, you create a file to store your password … Read more

How do I pass a local variable to a remote `Invoke-Command`? [duplicate]

To complement briantist’s helpful answer: The script block passed to Invoke-Command is (as intended) executed on the remote machine, using the remote machine’s variables by default. Thus, in order to use a local variable’s value,[1] extra steps are needed (to put it differently: inside a script block executed remotely, you cannot just refer to local … Read more