PowerShell 添加个性化驱动器


在PowerShell中可以通过虚拟驱动器来非常方便的访问函数,别名,注册表,证书等资源。能不能将个人和系统的一些特殊文件夹也加进虚拟驱动器中呢?这也是本篇的主题了。首先来看下默认的驱动器:

PS C:> Get-PSDrive

Name     Used (GB) Free (GB) Provider    Root               CurrentLocation
----     --------- --------- --------    ----               ---------------
Alias                        Alias
C            63.64    109.06 FileSystem  C:
cert                         Certificate
E           204.74     88.23 FileSystem  E:
Env                          Environment
Function                     Function
HKCU                         Registry    HKEY_CURRENT_USER
HKLM                         Registry    HKEY_LOCAL_MACHINE
Variable                     Variable
WSMan                        WSMan
Z                            FileSystem  Z:

上面提到的特殊文件夹诸如,桌面,我的图片,我的音乐,我的视频,Appdata,安装程序等,可以通过.NET中Environment.SpecialFolder枚举值获取。接下来是添加虚拟驱动器了,可以使用New-PSDrive命令,具体参加下面的函数:

function Add-SpecialDrive
{
  [System.Enum]::GetNames([System.Environment+SpecialFolder]) |
   % {
      $psName = $_
      $targetPath = [System.Environment]::GetFolderPath($_)
      New-PSDrive $psName FileSystem $targetPath
    }
}

上面的函数并不能万事大吉,因为创建的虚拟驱动器的作用域只在函数内部,需要改为:

function Add-SpecialDrive
{
  [System.Enum]::GetNames([System.Environment+SpecialFolder]) |
   % {
      $psName = $_
      $targetPath = [System.Environment]::GetFolderPath($_)
      New-PSDrive $psName FileSystem $targetPath  -Scope Global
    }
}

接下来就可以执行函数看看效果了。先执行Add-SpecialDrive函数,再查询一下当前支持的驱动器

PS C:> Get-PSDrive | select Name,Root

Name                   Root
----                   ----
AdminTools             C:UsersMosserAppDataRoamingMicrosoftWindowsStart MenuProgramsAdministrative ...
Alias
ApplicationData        C:UsersMosserAppDataRoaming
C                      C:
CDBurning              C:UsersMosserAppDataLocalMicrosoftWindowsBurnBurn
cert
CommonAdminTools       C:ProgramDataMicrosoftWindowsStart MenuProgramsAdministrative Tools
CommonApplicationData  C:ProgramData
CommonDesktopDirectory C:UsersPublicDesktop
CommonDocuments        C:UsersPublicDocuments
CommonMusic            C:UsersPublicMusic
CommonOemLinks         C:
CommonPictures         C:UsersPublicPictures
CommonProgramFiles     C:Program FilesCommon Files
CommonProgramFilesX86  C:Program Files (x86)Common Files
CommonPrograms         C:ProgramDataMicrosoftWindowsStart MenuPrograms
CommonStartMenu        C:ProgramDataMicrosoftWindowsStart Menu
CommonStartup          C:ProgramDataMicrosoftWindowsStart MenuProgramsStartup
CommonTemplates        C:ProgramDataMicrosoftWindowsTemplates
CommonVideos           C:UsersPublicVideos
Cookies                C:UsersMosserAppDataRoamingMicrosoftWindowsCookies
Desktop                C:UsersMosserDesktop
DesktopDirectory       C:UsersMosserDesktop
E                      E:
Env
Favorites              C:UsersMosserFavorites
Fonts                  C:WindowsFonts
Function
History                C:UsersMosserAppDataLocalMicrosoftWindowsHistory
HKCU                   HKEY_CURRENT_USER
HKLM                   HKEY_LOCAL_MACHINE
InternetCache          C:UsersMosserAppDataLocalMicrosoftWindowsTemporary Internet Files
LocalApplicationData   C:UsersMosserAppDataLocal
LocalizedResources     C:
MyComputer             C:
MyDocuments            C:UsersMosserDocuments
MyMusic                C:UsersMosserMusic
MyPictures             C:UsersMosserPictures
MyVideos               C:UsersMosserVideos
NetworkShortcuts       C:UsersMosserAppDataRoamingMicrosoftWindowsNetwork Shortcuts
Personal               C:UsersMosserDocuments
PrinterShortcuts       C:UsersMosserAppDataRoamingMicrosoftWindowsPrinter Shortcuts
ProgramFiles           C:Program Files
ProgramFilesX86        C:Program Files (x86)
Programs               C:UsersMosserAppDataRoamingMicrosoftWindowsStart MenuPrograms
Recent                 C:UsersMosserAppDataRoamingMicrosoftWindowsRecent
Resources              C:Windowsresources
SendTo                 C:UsersMosserAppDataRoamingMicrosoftWindowsSendTo
StartMenu              C:UsersMosserAppDataRoamingMicrosoftWindowsStart Menu
Startup                C:UsersMosserAppDataRoamingMicrosoftWindowsStart MenuProgramsStartup
System                 C:Windowssystem32
SystemX86              C:WindowsSysWOW64
Templates              C:UsersMosserAppDataRoamingMicrosoftWindowsTemplates
UserProfile            C:UsersMosser
Variable
Windows                C:Windows
WSMan
Z                      Z:

测试一下虚拟驱动器,”桌面”和”开始菜单”

PS C:> ls 'Desktop:Screen Recorder.lnk'

    Directory: C:UsersMosserDesktop

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/13/2012   6:14 PM       3081 Screen Recorder.lnk

PS C:> ls StartMenu:Programs

    Directory: C:UsersMosserAppDataRoamingMicrosoftWindowsStart MenuPrograms

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d-r--         7/14/2009  12:54 PM            Accessories
d-r--         7/13/2012   9:37 AM            Administrative Tools
d----         3/13/2012   1:00 PM            CodeFlow
d-r--         7/14/2009  12:49 PM            Maintenance
d----         8/31/2012  10:49 PM            Microsoft
d-r--         7/13/2012   9:37 AM            Startup
-a---          3/8/2012   2:36 PM       1413 Internet Explorer (64-bit).lnk
-a---          3/8/2012   2:36 PM       1447 Internet Explorer.lnk
-a---          3/8/2012   2:09 PM       3341 Remote Desktop Connection Manager.lnk
-a---         7/13/2012   6:14 PM       3041 Screen Recorder.lnk

还不错,最后再补充一点,这些新添加的全局虚拟驱动器仍然只对当前控制台会话有效,如果启用PowerShell 控制台,刚才添加的驱动器将不复存在。如果你每次都想打开控制台就让驱动器自动加载,可以参考系列文章之: Powershell自动执行脚本之profile

<pre>原文链接:http://powershell.com/cs/blogs/tips/archive/2012/09/04/adding-personal-drives-in-powershell.aspx

</pre>

×用微信扫描并分享
本文链接: https://www.pstips.net/powershell-add-special-vital-driver.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

电子邮件地址不会被公开。 必填项已用*标注