# Bypass ExecutionPolicy for dot-sourced script

**URL:** <https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587>\
**Category:** PowerShell Help\
**Created:** [February 15, 2024, 9:34am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587 "2024-02-15T09:34:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![EOberfrank](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@EOberfrank](https://forums.powershell.org/u/EOberfrank)\
**Post date:** [February 15, 2024, 9:34am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/1 "2024-02-15T09:34:50Z")

</div>

Hello together,  
I have a simple script which creates a xml file with user credentials. When I run it as admin it works fine, but when it comes to the general users, I run in two problems.

1. The user is not allowed to change the execution policy.
2. The network drive is not persistent and gets removed after the script ends.

For both problems I found solutions.

1. To change the ExecutionPolicy I just call the file with ExecutionPolicy Bypass from a script running with admin rights:

> Powershell.exe -ExecutionPolicy Bypass -File C:\MyScript\ConnectNetworkShare.ps1

1. To get a persistent network drive I just dot-source the script:

> . C:\MyScript\ConnectNetworkShare.ps1

Both work fine, but how can I get these two solutions together?  
Is there any way to set the ExecutionPolicy for the script to bypass and dot-source it at the same call?

Best regards  
EOberfrank

---

<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:** [February 15, 2024, 9:41am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/2 "2024-02-15T09:41:28Z")

</div>

Eike,  
Welcome to the forum. 👋🏼

> [@EOberfrank](#):
>
> which creates a xml file with user credentials

That sounds weird … are you using user credentials to create the XML file or do you create an XML file containing user credentials … ?? 🤔

> [@EOberfrank](#):
>
> The network drive is not persistent and gets removed after the script ends.

What exactly do you mean with “gets removed”? Are you talking about a mapped drive letter? If that’s the case whay not just using the UNC path instead?

---

<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:** [February 15, 2024, 10:07am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/4 "2024-02-15T10:07:28Z")

</div>

First of all …

When you post code please format it as code - NOT as quote.

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( \</\> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

[How to format code in PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) 1 \<---- Click 👆🏽 😉

( !! Sometimes the _preformatted text_ button hides behind the settings gear symbol. 😉 )

> [@EOberfrank](#):
>
> I create a xml file with the user credentials in it.

That sounds shady … may I ask whatfor do you do this?

> [@EOberfrank](#):
>
> To change the ExecutionPolicy I just call the file with ExecutionPolicy Bypass from a script running with admin rights:

Does the user have admin rights? If not - running the script with admin rights means running it in another context. Are you sure that’s what you need?

Regardless of that - encrypting a password using a secure string can only be reversed with the same user account ON THE SAME computer!!!

> [@EOberfrank](#):
>
> When I run the script not dot-sourced the network drive don’t show up in the explorer, because it is only available in the scope of the script itself. So after the scipt is over and closes the network drive is gone.

That’s why I recommended using the UNC path instead.

---

<div class="post-metadata">

**Author:** ![EOberfrank](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@EOberfrank](https://forums.powershell.org/u/EOberfrank)\
**Post date:** [February 15, 2024, 11:39am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/5 "2024-02-15T11:39:25Z")

</div>

Hi Olaf,  
thanks for the advice with the gear. I was looking for a button for code, but didn’t clicked on the gear. 😃

About the xml file:  
Before we used and cmd file with the “net use” command, but in that case the users had written their password unencrypted in the cmd.  
With the xml file it is not perfectly secured but much better than using a cmd file, because you need at least some encyption knowledge to decrypt the password string.  
And yes, only the specific users should be able to access the network share on their specific computers.

About the ExecutionPolicy:  
We use baramundi here to distribute software. With that software you can run powershell scripts on computers with admin rights. So I have this baramundi script with the above powershell script and can deploy it to the computers which are used by people who should get access to that network drive.

About the UNC path:  
You mean that I just create a shortcut to the specific folder… well that might work. I’ll try that.

But it would be interesting how to solve my problem if there are network drivers where they need to use specific users to access them. 🙂

---

<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:** [February 15, 2024, 3:04pm UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/6 "2024-02-15T15:04:10Z")

</div>

> [@EOberfrank](#):
>
> Before we used and cmd file with the “net use” command, but in that case the users had written their password unencrypted in the cmd.

I didn’t get yet why you need this at all. 🤷🏼‍♂️

> [@EOberfrank](#):
>
> But it would be interesting how to solve my problem if there are network drivers where they need to use specific users to access them.

If a user needs access to a particular drive or service the user should have the proper rights on the drive or the service. It shouldn’t be necessary to use other credentials than their own. 🤷🏼‍♂️

Are we talking about an Windows Active Directory Domain?

---

<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:** [February 16, 2024, 8:40am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/7 "2024-02-16T08:40:54Z")

</div>

> [@Olaf](#):
>
> That sounds weird … are you using user credentials to create the XML file or do you create an XML file containing user credentials … ?? 🤔

[Export-Clixml (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-clixml?view=powershell-7.4)

OP may be using this. On windows, it exports PSCredential Objects using WIndows DPAPI, so it’s encrypted by the user running it.

---

<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:** [February 16, 2024, 8:43am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/8 "2024-02-16T08:43:09Z")

</div>

> [@dotnVo](#):
>
> OP may be using this. On windows, it exports PSCredential Objects using WIndows DPAPI, so it’s encrypted by the user running it.

I know that. Thanks. I just wanted to point out, that you cannot use these encrypted passwords on other computers … a lot of people expect that … that’s all.

---

<div class="post-metadata">

**Author:** ![EOberfrank](https://avatars.discourse-cdn.com/v4/letter/e/f05b48/32.png) [@EOberfrank](https://forums.powershell.org/u/EOberfrank)\
**Post date:** [February 16, 2024, 11:13am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/9 "2024-02-16T11:13:28Z")

</div>

Hi Olaf,

first of all thank you.  
Your idea with and shortcut with an UNC-Links works fine.

Yes, we use Azure with Entra ID. There we also have some technical users which are used for specific purposes. So in some cases I have to connect with network drives with the credentials of the technical user.

Do I have to mark somehow that my problem is solve?  
Edit: Found it!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 17, 2024, 11:13am UTC](https://forums.powershell.org/t/bypass-executionpolicy-for-dot-sourced-script/23587/10 "2024-03-17T11:13:53Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
