# import-module to remote session

**URL:** <https://forums.powershell.org/t/import-module-to-remote-session/3615>\
**Category:** PowerShell Help\
**Created:** [January 12, 2015, 11:20pm UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615 "2015-01-12T23:20:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [January 12, 2015, 11:20pm UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/1 "2015-01-12T23:20:16Z")

</div>

I created a script that works on a local host relying on a custom module from codeplex. I turns out I need to run my script against a remote host as and when. Installing the module on remote hosts is out of question as they cannot store any extra software so I’m a bit stuck since I can’t see a way of using the module remotely. Is there a workaround?

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [January 12, 2015, 11:51pm UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/2 "2015-01-12T23:51:29Z")

</div>

You should be able to import the module on a remote share if you use CredSSP. Otherwise, you’ll be prevented from accessing the remote resource due to the double-hop issue.

---

<div class="post-metadata">

**Author:** ![jarek-c](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@jarek-c](https://forums.powershell.org/u/jarek-c)\
**Post date:** [January 13, 2015, 1:31am UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/3 "2015-01-13T01:31:14Z")

</div>

Thanks Will. The keyword got me in the right direction.

---

<div class="post-metadata">

**Author:** ![imessage357\_h](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@imessage357\_h](https://forums.powershell.org/u/imessage357_h)\
**Post date:** [January 14, 2015, 8:34am UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/4 "2015-01-14T08:34:38Z")

</div>

you can import a module form a remote machine like this

```
$dc = New-PSSession -ComputerName flldc01
import-module -name ActiveDirectory -PSSession $dc
```

or you can run a function on remote machine

```
$grb = (get-item Function:\Get-RecycleBin).scriptblock
invoke-command -ScriptBlock $grb -ComputerName $computers -Credential globomantics\jeff -hidecomputername | Sort Sum –Descending | Format-Table Sum,Count,Computername -Autosize
```

---

<div class="post-metadata">

**Author:** ![will-anderson](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/will-anderson/32/234_2.png) [@will-anderson](https://forums.powershell.org/u/will-anderson)\
**Post date:** [January 14, 2015, 11:46am UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/5 "2015-01-14T11:46:31Z")

</div>

For a module that’s installed on the remote machine, you’d be correct HMan. Unfortunately, in his original post he mentioned that he can’t install the module locally, therefore he’d have to access the module remotely from the remote machine. Thus he’ll run into the double-hop issue even from the PSSession.

YourLocalComputer -\>RemoteComputer-\>RemoteComputerWithModule

He’ll get an error attempting to access the remote resources. [PowerShell.org](http://PowerShell.org) has an excellent free resource, Secrets of PowerShell Remoting. It’s a really good read. 🙂

[https://powershell.org/ebooks/](https://powershell.org/ebooks/)

---

<div class="post-metadata">

**Author:** ![\_timpringle](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/_timpringle/32/276_2.png) [@\_timpringle](https://forums.powershell.org/u/_timpringle)\
**Post date:** [January 14, 2015, 7:11pm UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/6 "2015-01-14T19:11:40Z")

</div>

If don’t mind a bit of dirty coding, you could do something like the following, which I’ve used from time to time. It’s really pretty gross, so get the sickbag ready.

Use get-content to read the module into a string variable (you may need to use $variablename = get-content | out-string to maintain formatting)  
Use get-content to read your script into a string variable (as above re formatting)  
Use invoke-command with the -computername parameter, and also using -scriptblock  
In the script block, refer to the variables via $using:variablename, and write them to a temporary folder via Set-Content (you may need to use $variablename | out-string | set-content to retain formatting)  
Dot source the module file you’ve created in the temp folder  
Run the script file you’ve created in the temp folder  
Remove the files you’ve created in the temp folder

For a completely non file writing option, you should actually be able to use Invoke-Expression within the scriptblock without the need to write the files to disk, but it’s something I’ve not yet tried.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:46pm UTC](https://forums.powershell.org/t/import-module-to-remote-session/3615/7 "2024-05-16T20:46:32Z")

</div>


