How to remove mapped network drive with Powershell? - powershell

I am trying to create and remove a new mapped network drive using PowerShell.
It creates the mapped drive, however I can't seem to remove the mapped drive. The error message I receive is:
Dir : Cannot find path 'C:\Windows\system32\P' because it does not exist.
New-PSDrive -Name "P" -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem"
#Get-PSDrive P | Remove-PSDrive
#Remove-PSDrive -Name P -Force
#Remove-PSDrive P, Z
All Google and Stack Overflow has suggested to me thus far is using the commands that I have previously commented out. I am unsure of what I am doing wrong but had a feeling it could be done to the location of my files perhaps?
All help would be greatly appreciated!

The error is because you're running dir P instead of dir P: You need the : to signify a drive not a folder.
dir (which in Powershell is a actually an alias for Get-ChildItem) can read multiple areas of the OS so you need to be more specific with what you tell it.
Examples:
File system: Get-ChildItem C:
Registry: Get-ChildItem HKCU:
Certificate Store: Get-ChildItem cert:
Whilst with Get/Remove-PSDrive commands you are specifically telling it you want a "FileSystem" drive so it knows that Name is a drive letter.
With regards to removing the drive, either of the two commands you've listed will work fine:
New-PSDrive -Name P -Root "\\VM-Blue-Robin\Testing" -Persist -PSProvider "FileSystem"
Get-PSDrive P | Remove-PSDrive
Remove-PSDrive -Name P -Force

Related

Empty folders when using Copy-Item Network Drive to Local Drive

I'm currently using this Powershell script
Function Copy-ItemUNC {
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files';
}
When I run the script it creates the folders but there are no subfolders within them.
Second problem I have is that I have to manually open Windows Explorer and type in the path to connect to it first in order for this script to even run. Is there a way to fix that too?
It's because you are not asking for subdirs, as that requires you to do this.
Copy-Item -Recurse
-Recurse <SwitchParameter>
Indicates that this cmdlet performs a recursive copy.
Required? false
Position? named
Default value False
Accept pipeline input? False
Accept wildcard characters? false
(Get-Command -Name Copy-Item).Parameters.Keys
Get-Help -Name Copy-Item -Full
Get-Help -Name Copy-Item -Examples
This command copies the mar1604.log.txt file to the C:\Presentation directory. The command does not delete the original file.
Example 2: Copy the contents of a directory to another directory
PS C:\>Copy-Item "C:\Logfiles" -Destination "C:\Drawings" -Recurse
So, this line should be like this..
Copy-Item -Path "\\servername\serverupdates\deploy\Program Files\*" -Destination 'C:/Program Files' -Recurse
You also do not really need this..
New-PSDrive -Name "B" -PSProvider "FileSystem" -Root "\\ServerName\serverupdates\deploy\Program Files\"
... based on what you are after. Especially since you are not using it anywhere in your code. That semi-colon is also not needed.

How To Transfer File Between 2 PSDrives

I created two PSDrives on my client computer PowerShell session to two different Remote Servers.
New-PSDrive -Name DllFrom -PSProvider FileSystem -Root "\\WPDHSFMSLxx\adap\Database\Install\KareAssistTest\HIDn"
New-PSDrive -Name DllTo -PSProvider FileSystem -Root "\\WTDHSAPPLxx\d\ServerDLLDev"
I can dir either one and contents are displayed.
I can't copy a text file between these 2 drives using Copy-Item:
PS C:\WINDOWS\system32> Copy-Item DllFrom/HelloWorld.txt DllTo/HelloWorld.txt
Copy-Item : Cannot find path 'C:\WINDOWS\system32\DllFrom\HelloWorld.txt' because it does
At line:1 char:1
+ Copy-Item DllFrom/HelloWorld.txt DllTo/HelloWorld.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\WINDOWS\syst...\HelloWorld.txt:String) [
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I changed the location to the DLLFrom drive and tried it.
Set-Location DllFrom:
PS DllFrom:\>Copy-Item HelloWorld.txt DllTo
and
PS DllFrom:\> Copy-Item \\WPDHSFMSL03\adap\Database\Install\KareAssistTest\HIDn\HelloWorld.txt DllTo
Nothing happened, command line refreshed, but no file showed up.
DllFrom and DllTo are drives, and need a colon in their name when you refer to them. You create them with a -Name without the colon, but later use it when using the drive, just like a single letter drive C: or other providers like HKCU:
Try: Copy-Item DllFrom:/HelloWorld.txt DllTo:/HelloWorld.txt
There is more overview on PSDrives at 4sysops. From the linked page:
Remember that the PSDrive name does not include the colon (:) but you need to include it when using the drive to set location.
When you did Copy-Item HelloWorld.txt DllTo, you'll find a copy of HellowWorld.txt in your current directory named "DllTo"

Using a mapped drive as a variable

We're looking at swapping a file server with a newer one. The new one will have a new IP and name.
My aim is to remove the mapped drive our users have for the current server and replace it with a new one. The problem is our users have it on different letters. So I need to run a script that will replace the UNC path regardless of drive letter.
My script so far can find me the drive letter, but it's not removing the mapping.
--
Get-PSDrive | ForEach {
If ( $_.DisplayRoot -eq '\\OLDSERVER\PATH' ) {
Remove-PSDrive -Name $_.Name
New-PSDrive –Name $_.Name –PSProvider FileSystem –Root "\\NEWSERVER\PATH" –Persist
}
}
Powershell cannot remove a mapping it didnt put in. I've changed it to a net-use command and it works.

PowerShell map persistent sharepoint path

I'm trying to map the path to a SharePoint document library in a persistent way. It's strange that this works fine:
$Sharepoint = '\\domain.net\stuff\Documents\Folders - Permission matrix'
New-PSDrive -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials
But this doesn't work:
New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credential $Credentials
New-PSDrive : The network resource type is not correct
At line:1 char:1
+ New-PSDrive -Persist -Name P -Root $Sharepoint -PSProvider FileSystem -Credentia ...
The commands are both using the same PSProvider but one is persistent and the other one not. How can I have this persistent without reverting to net use?
I ran into this problem a few weeks back in a script which mysteriously stopped worked whilst I was developing it, seems to be a Windows error 66 rather than Powershell as explained here
Here is an alternative to net use which uses credentials
# map drive using credentials
(New-Object -ComObject WScript.Network).MapNetworkDrive("$LocalDrive","\\$computer\$drive",$false,$($credentials.username),$($credentials.GetNetworkCredential().password))
I tend to use PSDrive like this
# discover and delete
if (Get-PSDrive -Name Results -ErrorAction SilentlyContinue) { Remove-PSDrive -Name Results -Scope Global | Out-Null }
# create using credentials
if ((New-PSDrive -Name Results -PSProvider FileSystem -Root "\\server\share" -Credential $Credentials -Scope Global -ErrorAction SilentlyContinue) -eq $null) { $WSHShell.popup(“You do not have access to the results repository“,0,””,0) | Out-Null }
# call from a separate function
(Get-PSDrive -Name Results).root
It might just be that a reboot will solve the problem because I cannot recreate the issue today.
I had a similar issue, turns out Windows 2016 and 2019 needs WEBDav Redirection installed.
I was getting error 53, when trying to map a SharePoint library.

open folder from Network with Powershell

I would like to open a txt.File from a sharedFolder with Powershell. The problem is, that it has to run whether an user is logged on or not. I'm looking for something like net-use.
The Program should also check, whether the psDrive exists or not.
Is it possible if I do that like this?
new-psdrive -name Z -psprovider FileSystem -root \\vcs.view
It works like that:
I map and then I check whether the file exists:
#mapping
try
{
new-psdrive -name Z -psprovider FileSystem -root $ShareFolder
}
catch
{
echo "WriteMessageTOAdmin ERROR!!"
exit
}
$folderPath = "Z:\users.txt"
if(!(test-Path -path $folderPath))
{
echo "WriteMessageTOAdmin ERROR!!"
exit
}
You don't need to map a network share to open a file on a network share:
Get-Content \\server\sharename\foo.txt
Works just fine as does using Test-Path on a UNC path e.g.
Test-Path \\server\sharename\foo.txt
Is there a reason you need to map the share to a local drive?