# Changing directory based on creation of network mapped drive

**URL:** <https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460>\
**Category:** PowerShell Help\
**Created:** [June 1, 2020, 4:18am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460 "2020-06-01T04:18:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrejch](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@andrejch](https://forums.powershell.org/u/andrejch)\
**Post date:** [June 1, 2020, 4:18am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/1 "2020-06-01T04:18:38Z")

</div>

Hi guys,

I am trying to map a network drive with a randomly generated letter.

```
$drive = Get-ChildItem function:[f-z]: -n | Where { !(Test-Path $_) } | select -First 1
$env = New-PSDrive -Name ($drive)[0] -PSProvider "FileSystem" -Path
```

After that I want to change the directory to be the newly created drive. What I am doing:

```
cd $drive:
$someEnv = $drive + ":\include"
```

Changing the directory is not working. And I guess the second line too.

Can you please help me how I can change the directory successfully based on the network drive name and then in$someEnv store for example if the network drive is X:

X:\include

&nbsp;

Thanks in advance.

Regards

---

<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:** [June 1, 2020, 4:48am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/2 "2020-06-01T04:48:52Z")

</div>

Don’t you get errors? [New-PSDrive](https://docs.microsoft.com/de-de/powershell/module/Microsoft.PowerShell.Management/New-PSDrive?view=powershell-7) does not have a parameter **-Path**. You have to provide a value for the parameter **-Root**. Please read the complete help to learn how to use it. And you sould not use **ENV** as a variable name as it is a reserved Powershell variable - [About Environment Variables](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_environment_variables?view=powershell-7). And as you already have only one drive in your variable **$drive** you should not use **($drive)[0]** as this cuts off the colon from your drive name. 😉

---

<div class="post-metadata">

**Author:** ![andrejch](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@andrejch](https://forums.powershell.org/u/andrejch)\
**Post date:** [June 1, 2020, 5:07am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/3 "2020-06-01T05:07:22Z")

</div>

Thanks Olaf,

Sorry my mistake with -Path. I was removing some things from the command because of privacy. This is the correct command:

```
$envPath = New-PSDrive -Name ($drive)[0] -PSProvider "FileSystem" -Root SomeNetworkPath
```

You are suggesting that I should structure it like this(without [0])?

```
$envPath = New-PSDrive -Name $drive -PSProvider "FileSystem" -Root SomeNetworkPath
```

What do you think about changing the directory like this:

```
cd $drive:
$someEnv = $drive + ":\include"
```

The thing is I am using Powershell and Jenkins and can’t test everything locally, but checking the output directly in Jenkins log.

P.S I am not using $ENV in my code, its just here for demonstration purposes. 🙂

---

<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:** [June 1, 2020, 5:23am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/4 "2020-06-01T05:23:05Z")

</div>

> [@](#):
>
> You are suggesting that I should structure it like this(without [0])?

Sorry, my mistake. That’s actually right like you did it. But because your variable **$drive** already includes the colon your following commands should cause errors actually.

---

<div class="post-metadata">

**Author:** ![andrejch](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@andrejch](https://forums.powershell.org/u/andrejch)\
**Post date:** [June 1, 2020, 5:33am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/5 "2020-06-01T05:33:52Z")

</div>

You are correct. I fixed that. Thank you.

One last question, how I should remove the created **$drive** at the end. What I am trying:

```
$someEnv = Remove-PSDrive -Name $drive
```

Here I think removing of the drive will fail because it will be for example “X:” instead of “X”.

Am I correct?

&nbsp;

---

<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:** [June 1, 2020, 7:52am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/6 "2020-06-01T07:52:42Z")

</div>

Why don’t you use the same syntax you used to create the mapped drive? 😉

BTW:  
When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.

[https://stackoverflow.com/questions/62132100/how-to-remove-mapped-network-drive-when-created-with-a-random-letter](https://stackoverflow.com/questions/62132100/how-to-remove-mapped-network-drive-when-created-with-a-random-letter)

Thanks

---

<div class="post-metadata">

**Author:** ![andrejch](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@andrejch](https://forums.powershell.org/u/andrejch)\
**Post date:** [June 1, 2020, 11:12am UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/7 "2020-06-01T11:12:19Z")

</div>

I agree, even though the context of the two posts is different. The original post here is about the changing the directory and you helped a lot. I didn’t had much time that’s why I didn’t referenced this one on the stackoverwflow.

Thanks for your help and will do that in future.

Regards and keep up the good work. 🙂

---

<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:29pm UTC](https://forums.powershell.org/t/changing-directory-based-on-creation-of-network-mapped-drive/14460/8 "2024-05-16T20:29:26Z")

</div>


