通过 Powershell 配置 Windows Explorer 文件夹选项

Posted

技术标签:

【中文标题】通过 Powershell 配置 Windows Explorer 文件夹选项【英文标题】:Configure Windows Explorer Folder Options through Powershell 【发布时间】:2011-05-28 09:06:43 【问题描述】:

我正在寻找一种方法来通过 Powershell 在 Windows 资源管理器的文件夹选项对话框中配置一些选项。

选项有:

选择“显示隐藏的文件、文件夹和驱动器” 取消选中“隐藏已知文件类型的扩展名” 取消选中“隐藏受保护的操作系统文件(推荐)”

【问题讨论】:

【参考方案1】:

Keith's answer 开箱即用并不适合我。唯一需要修改注册表值的是 ShowSuperHidden。 Hidden(显示隐藏文件...)和 HideFileExt(隐藏文件扩展名)在我打开文件夹设置中的查看选项卡后立即恢复为之前的值。

这是我经过反复试验后找到的解决方案(explorer.exe 会自动重新启动):

$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Set-ItemProperty $key Hidden 1
Set-ItemProperty $key HideFileExt 0
Set-ItemProperty $key ShowSuperHidden 1
Stop-Process -processname explorer

我在 Windows Server 2008 R2 和 Windows 7 上对此进行了测试。

【讨论】:

同一类别中的另一个,虽然不是主题启动者特别要求的: Set-ItemProperty $key TaskbarGlomLevel 2 这将禁用任务栏上类似打开的应用程序的分组。这还需要重新启动资源管理器进程才能应用。【参考方案2】:

示例windows registry(article) 脚本:

Windows Registry Editor Version 5.00

[hkey_current_user\software\microsoft\windows\currentversion\explorer\advanced]

;hide empty drives [uncheck]
"hidedriveswithnomedia"=dword:00000000

;hide extensions for known file types [uncheck]
"hidefileext"=dword:00000000

;show hidden files, folders, and drives [check]
"showsuperhidden"=dword:00000001

;hide folder merge conflicts [uncheck]
"hidemergeconflicts"=dword:00000000

;hide protected operating system files (recommended) [uncheck]
"hidden"=dword:00000001

;use check boxes to select items [check]
"autocheckselect"=dword:00000001

另存为*.reg 文件,并通过单击它并确认操作或通过在文件上发出reg /import (examples) 命令来导入。

ps:不需要explorer 或系统重启

【讨论】:

都可以从powershell命令行运行:reg import file.reg【参考方案3】:

我相信这些对应于注册键 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced 下的注册表项。您可以使用 Set-ItemProperty cmdlet 来更改它们的值,例如:

$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Set-ItemProperty $key ShowSuperHidden 1

本地机器似乎也有一个对应的密钥(与上面的每个用户设置相反):HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder

【讨论】:

如果用户和本地机器不同怎么办?哪个优先? 我试过了,似乎用户设置优先,我们需要更改创建用户配置文件时使用的默认值。它们可能位于不同的注册表位置【参考方案4】:

上述注册表补丁是正确的,但它们并不能解决整个问题。这是我使用的脚本。它遍历注册表和配置文件目录中的所有用户(包括 DEFAULT,因此新创建的用户也可以获取它们)并为它们设置这些选项。

REM Changes to HKLM are not user-specific

REM Turns "hide file extensions" OFF and "show hidden files" ON.
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\HideFileExt /v DefaultValue /t REG_DWORD /d 0 /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\SHOWALL /v DefaultValue /t REG_DWORD /d 1 /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v HideFileExt /t REG_DWORD /d 0 /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v Hidden /t REG_DWORD /d 1 /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v ShowSuperHidden /t REG_DWORD /d 1 /f
REG ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced /v DontPrettyPath /t REG_DWORD /d 1 /f

REM Get path to "Users" dir.
echo WScript.Echo CreateObject("WScript.Shell").RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProfileList\ProfilesDirectory") >%temp%\profpath.vbs
for /f "tokens=*" %%i in ('cscript //nologo %temp%\profpath.vbs') do set ProfPath=%%i
del /q %temp%\profpath.vbs


REM Modifies registry keys in for all logged in users
REM Also modify it in the .DEFAULT hive so future users get it.
REM Also edits the registry hive for users who are not logged in
REM This section Copyright Jared Barneck
REM Modified by Ken Carlilep0 and Sam Hills

FOR /F "tokens=2* delims=\" %%a IN ('REG QUERY HKU ^|Findstr /R "DEFAULT S-1-5-[0-9]*-[0-9-]*$"') DO CALL :modkey %%a
For /d %%b in ("%ProfPath%\*") do call :modlokey "%%b"
@REM Exiting here ends the whole batch file.
EXIT /B 0


REM Modify logged-out users
:modlokey
  set RegFile=%~1\ntuser.dat
  REG LOAD HKU\TempHive "%RegFile%">NUL 2>&1
  call :modkey TempHive
  REG UNLOAD HKU\TempHive >NUL 2>&1
EXIT /B 0

REM Modifications to HKEY_USERS go here:
:modkey
REM Turns "hide file extensions" OFF and "show hidden files" ON.
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "HideFileExt" /t REG_DWORD /d "0" /f
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "Hidden" /t REG_DWORD /d "1" /f
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "ShowSuperHidden" /t REG_DWORD /d "1" /f
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "DontPrettyPath" /t REG_DWORD /d "1" /f

REM Combine taskbar buttons only when taskbar is full
REM 0 = Always combine, hide labels, 1 = Combine when taskbar is full, 2 = Never combine
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "TaskbarGlomLevel" /t REG_DWORD /d "1" /f
REM Enable this line if you use multiple monitors:
REM  REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v "MMTaskbarGlomLevel" /t REG_DWORD /d "1" /f

REM Don't add "- Shortcut" to new shortcuts
REG ADD "HKU\%1\Software\Microsoft\Windows\CurrentVersion\Explorer" /v "link" /t REG_BINARY /d 00000000 /f

REM Turns on "Computer" Desktop Icon
REG ADD HKU\%1\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\NewStartPanel /v "20D04FE0-3AEA-1069-A2D8-08002B30309D" /t REG_DWORD /d 0 /f
REG ADD HKU\%1\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\HideDesktopIcons\ClassicStartMenu /v "20D04FE0-3AEA-1069-A2D8-08002B30309D" /t REG_DWORD /d 0 /f

@REM Exiting here only ends this instance of the call to the
@REM :modkey label. It does not end the whole batch file.
EXIT /B 0

【讨论】:

【参考方案5】:

使用 Windows 10 (v1703-1809) 上的 Powershell 使用更多信息更新此内容,我能够使用以下代码为当前用户和本地计算机引用和设置文件夹选项注册表项。

对我来说最大的认识,在以前的帖子中并不明显,是文件夹选项相关设置的 reg 键路径根据您是否要获取/设置略有不同本地机器或当前用户,无论是键路径一致性还是键值访问。此外,如果不明显,当前用户设置将覆盖本地计算机。

这是一个示例代码 sn-p(使用 PS 5.1 测试):

## Grab Current User setting(s):
$CUfvHidden      = (Get-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name 'Hidden').Hidden
$CUfvHideFileExt = (Get-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name 'HideFileExt').HideFileExt
$CUfvFullPath    = (Get-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\CabinetState -Name 'FullPath').FullPath
if ($CUfvHidden -eq 1)  Write-host "CU: Show Hidden set to 'ON'"  #expecting val 1 or 2
else  Write-host "CU: Show Hidden set to 'OFF'" 
if (-not $CUfvHideFileExt)  Write-host "CU: File extensions DISPLAYED"  #expecting val 1 or 0
else  Write-host "CU: File extensions hidden" 
if ($CUfvFullPath)  Write-host "CU: SHOW full path in title bar"  #expecting val 1 or 0
else  Write-host "CU: DO NOT show full path in title bar" 

## Grab Local Machine setting(s)...As you can see the LM reference paths are
## slightly different, to get 1 and 0 values, compared to CU and each other:
$LMfvHidden      = (Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\Hidden\ShowAll).CheckedValue
$LMfvHideFileExt = (Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\HideFileExt).CheckedValue
$LMfvFullPath    = (Get-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Folder\ShowFullPath).CheckedValue
if ($LMfvHidden)  Write-host "LM: Show Hidden set to 'ON'"  #expecting val 1 or 2
else  Write-host "LM: Show Hidden set to 'OFF'" 
if (-not $LMfvHideFileExt)  Write-host "LM: File extensions DISPLAYED"  #expecting val 1 or 0
else  Write-host "LM: File extensions hidden" 
if ($LMfvFullPath)  Write-host "LM: SHOW full path in title bar"  #expecting val 1 or 0
else  Write-host "LM: DO NOT show full path in title bar" 

【讨论】:

以上是关于通过 Powershell 配置 Windows Explorer 文件夹选项的主要内容,如果未能解决你的问题,请参考以下文章

使用Powershell配置Windows Failover Cluster群集

powershell 使用PowerShell配置IP地址(在Windows上)

练习0--配置环境

Windows10 IoT开发系列PowerShell的相关配置

使用powershell通过配置文件config调用wcf(含用户名密码认证)

powershell Boxstarter Windows 10配置