# Executing Powershell script remotely without installing modules?

**URL:** <https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868>\
**Category:** PowerShell Help\
**Created:** [July 12, 2022, 2:27pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868 "2022-07-12T14:27:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [July 12, 2022, 2:27pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/1 "2022-07-12T14:27:07Z")

</div>

Hi Everyone,

I need some help and example to allow a Specific PowerShell script to be executed remotely on the server from my local PC.

The goal is so that I can centralize the execution and the PowerShell modules installation in one single server.

This is the script that I’m currently working with:

```auto
$RemoteServer = "Remote-SVR1" + "." + (Get-WmiObject Win32_ComputerSystem).Domain
$session = New-PSSession -ComputerName $RemoteServer

Invoke-Command -Session $session -ScriptBlock {
		Import-Module -Name 'Microsoft.Graph' -Force -ErrorAction Stop
		Import-Module -Name 'ActiveDirectory' -Force -ErrorAction Stop
		
		Read-Host -Prompt $Prompt -AsSecureString
		Read-Host "Please enter input ..."
		
		#Do some proessing and then export to .CSV
		$paramExportCsv = @{
			Path = 'Path'
			Delimiter = $Delimiter
			InputObject = $InputObject
			LiteralPath = 'LiteralPath'
			Force = $true
			NoClobber = $true
			Encoding = 'Unicode'
			Append = $true
			NoTypeInformation = $true
		}
		
		Export-Csv @paramExportCsv
		
		# Send Email when all is completed...
		$paramSendMailMessage = @{
			To = 'To'
			Subject = 'Subject'
			Body = 'Body'
			SmtpServer = 'SmtpServer'
			From = 'From'
			Attachments = 'Attachments'
			Bcc = 'Bcc'
			BodyAsHtml = $true
			Encoding = $Encoding
			Cc = 'Cc'
			DeliveryNotificationOption = 'None'
			Priority = 'Normal'
			Credential = $Credential
			UseSsl = $true
			Port = $Port
		}
		
		Send-MailMessage @paramSendMailMessage
		
}

Remove-PSSession $session

```

Any help and suggestion would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 12, 2022, 3:27pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/2 "2022-07-12T15:27:31Z")

</div>

Albert,  
Welcome back to the forum. 👋🏽

> [@albertwt](#):
>
> … centralize … the PowerShell modules installation in one single server.

That’s most of the times not as easy or helpful as you might think. 😉

You cannot use a module on a remote computer you installed on your local machine out of the box. You could use implicit remoting but that’s actually made for the other way around. Using a module on your local computer you have installed on a remote computer. And if you want to use this on a remote computer you’d have to deal with the so called double hop issue.

A much better option would be to have a server with all needed modules and run the necessary scripts there.

And BTW: in the code you shared you’re not using the modules you import at the beginning of your script block. 😉

Here you have some more information about implicit remoting and about how to circumvent the double hop issue:

[https://4sysops.com/archives/using-powershell-implicit-remoting](https://4sysops.com/archives/using-powershell-implicit-remoting)

> **[Remoting the Implicit Way - Scripting Blog \[archived\]](https://devblogs.microsoft.com/scripting/remoting-the-implicit-way/)**
>
> Summary: Guest blogger, June Blender, talks about how to use Windows PowerShell implicit remoting. Today we welcome June Blender, senior programming writer for Windows Azure Active Directory. Take it away, June… Just about everyone knows how to run...

> **[Making the second hop in PowerShell Remoting - PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.2)**
>
> This article explains the various methods for configuring second-hop authentication for PowerShell remoting, including the security implications and recommendations.

[https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp](https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp)

---

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [July 14, 2022, 2:19pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/3 "2022-07-14T14:19:49Z")

</div>

Hi @Olaf,  
Yes, it’s been a while since I posted something back in this forum.

Thank you for your suggestion, it looks like it is not possible without deploying an additional server or Remote Desktop Session Host (RDSH) like in this article: [Remote Desktop Services (RDS) – PowerShell App Launch and File Associations Expanded - PowerShell](http://masteringposh.com/remote-desktop-services-rds-powershell-app-launch-and-file-associations-expanded)

Please correct me if I’m wrong, by publishing the script on the RDSH server, the modules can stay there and then be updated manually as required.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 14, 2022, 2:25pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/4 "2022-07-14T14:25:41Z")

</div>

I’d say it does not have to be a Remote Desktop Session Host and it does not even have to be a server but yes - that’s the idea. You have one machine with all needed modules and you run all queries from there.

---

<div class="post-metadata">

**Author:** ![albertwt](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/albertwt/32/2550_2.png) [@albertwt](https://forums.powershell.org/u/albertwt)\
**Post date:** [July 14, 2022, 2:38pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/5 "2022-07-14T14:38:12Z")

</div>

thanks for the confirmation and the explanation @Olaf 🙂

---

<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:17pm UTC](https://forums.powershell.org/t/executing-powershell-script-remotely-without-installing-modules/19868/6 "2024-05-16T20:17:43Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


