Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function foo on my local Machine. (In my profile, but calling c:\scripts\foo.ps1 would be also OK!) How do i load this on ServerA and ServerB so i can execute the function in the next statement?

This is what i tried with no success..

$serverlist = 
foreach ($item in $serverlist) {
    New-PSSession -ComputerName "$item" -Credential $cred -Name  ($item + "_session")
    Invoke-Command -ComputerName $item -Credential $cred -filepath scripts:\foo.ps1 
    Invoke-Command -ComputerName $item -Credential $cred -scriptblock {foo}  
share|improve this question

Two mistakes here:

  1. First of all, Invoke-Command can use either the -ComputerName parameter or the -Session parameter. You are creating a session here but are not using it, therefore the commands run are executed in different sessions and don't know anything about each other.

  2. -ComputerName takes a string[] so you could drop the foreach. You can then probably just put the function definition and the call in a single script block.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.