The observation of PowerShell small technique UNC path _powershell

Source: Internet
Author: User

Many commands support UNC paths, but UNC paths look strange in scripts. Look at the following code:

ps> Test-path-path \\127.0.0.1\c$
True

It returns true, proving that a UNC path exists. Then change the current path to a non-file system path and then test:

ps> CD hkcu:\
 
ps> test-path-path \\127.0.0.1\c$
False

The same path but now it has failed. This is because the UNC path does not contain the letter, and PS needs to allocate the current position according to the disk character. If the current path is not included in the disk path, PowerShell assumes the currently selected location. So when you change the current directory to the registry, PS will try to get the UNC path from the current registry, which is the reason for the error.
Even worse, there are some inexplicable reasons why PS may confuse you when you are using "net use" as a network map to view the driver commands.
Here's a simple workaround, when you use a UNC path in a command, consider the current UNC path location first, which will avoid this problem:

ps> test-path-path filesystem::\\127.0.0.1\c$
True
 
ps> cd hkcu:\ ps>
 
\ 127.0.0.1\c$
False
 
ps> test-path-path filesystem::\\127.0.0.1\c$
True

If you are concerned about the "Net use" to create trouble, please advance the "filesystem::" Do the appropriate measures. will be able to solve the problem thoroughly.

Supports all PS versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.