This works in Windows 10 using AutoHotkey:
Save the file as:
Show Hide Hidden Files Refresh Explorer Windows.ahk
#SingleInstance Force
#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe
^h::
If (!A_IsAdmin) ; IF NOT Admin
{
Run, *RunAs "%A_ScriptFullPath%" ; Run script as admin
ExitApp ; Exit the current instance running without admin privileges
}
RegRead, hidden, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden ; Get If Files Hidden Or Shown
If hidden contains 1 ; IF Files NOT Hidden
{
RegWrite, REG_DWORD, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 2 ; Hide Hidden Files
}
Else ; ELSE Files Hidden
{
RegWrite, REG_DWORD, HKCU, Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced, Hidden, 1 ; Show Hidden Files
}
WinGet, windows, List, ahk_exe explorer.exe ; Get List Explorer Windows
Loop, %windows%
{
id := windows%A_Index% ; id
WinGetTitle, title, ahk_id %id% ; title
WinGetClass, class, ahk_id %id% ; class
WinGet, exe, ProcessName, ahk_id %id% ; exe
ControlSend, DirectUIHWND2, {F5}, %title% ahk_class %class% ahk_exe %exe% ; Refresh All Explorer Windows
}
Return
#IfWinActive
#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe ensures that the script will be run only if Explorer window is active.
^h:: is the Ctrl+H Hotkey which triggers the code. You can set it to whatever you like.
The script needs to run as admin so that it can write to registry.
A_IsAdmin is a built in variable that returns 1 if current user has admin rights, otherwise returns 0.
- It first checks if user is NOT admin (
!A_IsAdmin).
- If user is not admin, it runs the same script which it gets the full path with the built in variable
"%A_ScriptFullPath%". The *RunAs parameter runs the script as admin.
ExitApp exits the current script if it does not have admin rights.
- The
Run command runs the script again with admin rights, now the script has admin rights, it skips the IF condition and executes code below.
RegRead reads the registry entry that shows if hidden files are hidden or shown. If the value is 1 then it hides the hidden files, otherwise it shows them.
WinGet, windows gets the list of all Explorer windows.
Loop loops through all Explorer windows, gets the title, class and process of each window, and ControlSend, DirectUIHWND2, {F5} refreshes all Explorer windows so that the changes are reflected (files are shown or hidden).