# Save target key with takeownership right $targetkey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AlternateShells\AvailableShells", [Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree, [System.Security.AccessControl.RegistryRights]::takeownership) # Obtain access control, owner, and rule of target key $targetac = $targetkey.GetAccessControl() $targetowner = $targetac.GetOwner([System.Security.Principal.NTAccount]) $targetrule = $targetac.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) # Obtain access control, owner, and rule of normal key $adminac = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\").GetAccessControl() $adminowner = $adminac.GetOwner([System.Security.Principal.NTAccount]) $adminrule = $adminac.GetAccessRules($true, $true, [System.Security.Principal.NTAccount]) # Set owner and permission of target key $targetac.SetOwner($adminowner) $targetac.SetAccessRule($adminrule.Item(0)) $targetkey.SetAccessControl($targetac) # Change key from 40000 to 20000 Rename-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AlternateShells\AvailableShells" -Name 40000 -NewName 20000 # Restore owner and permission of target key $targetac.SetAccessRule($targetrule.Item(2)) $targetac.SetOwner($targetowner) $targetkey.SetAccessControl($targetac)
Access Registry with PowerShell
Working with Registry is very similar to working with files and folders with PowerShell.
Set-Location cmdlet can set the current working location to registry.
PS C:\Windows\system32> Set-Location Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER PS Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER>
The registry provider’s full name is Microsoft.PowerShell.Core\Registry, but this can be shortened to just Registry.
PS C:\Windows\system32> Set-Location Registry::HKEY_CURRENT_USER PS Microsoft.PowerShell.Core\Registry::HKEY_CURRENT_USER>
In addation, PowerShell has two pre-defined registry ‘drives’:
HKLM for HKEY_LOCAL_MACHINE
HKCU for HKEY_CURRENT_USER
PS C:\Windows\system32> Set-Location HKCU: PS HKCU:\>
However, other registry roots are not defined.
PS HKLM:\> Set-Location HKCR:
Set-Location : 드라이브를 찾을 수 없습니다. 이름이 'HKCR'인 드라이브가 없습니다.
위치 줄:1 문자:1
+ Set-Location HKCR:
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (HKCR:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
PS HKLM:\>
You can define other roots by yourself with New-PSDrive cmdlet.
PS HKLM:\> New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT Name Used (GB) Free (GB) Provider Root CurrentLocation ---- --------- --------- -------- ---- --------------- HKCR Registry HKEY_CLASSES_ROOT PS HKLM:\> Set-Location HKCR: PS HKCR:\>
Modify File Time Stamps using PowerShell
Usages: (Get-Item [filename]).CreationTime = [date/time] (Get-Item [filename]).LastAccessTime = [date/time] (Get-Item [filename]).LastWriteTime = [date/time] PS C:\> (Get-Item .\test.txt).LastAccessTime = Get-Date → This command converts 'last access time' of 'test.txt' to current date and time PS C:\> (Get-Item .\christmas.tree).LastWriteTime = '2016-12-25 AM0:00' → This command converts 'last write time' of 'christmas.tree' to specific date and time
Batch Processing with ForEach: Get-ChildItem | ForEach-Object { $_.LastWriteTime = Get-Date } PS C:\> Get-ChildItem | ForEach-Object { $_.LastWriteTime = Get-Date } → This command converts 'last write time' to current date and time of all files in working directory PS C:\> $i = 0; Get-ChildItem | ForEach-Object { $_.LastWriteTime = (Get-Date).AddDays(-1).AddMinutes($i++) } → This command converts 'last write time' to yesterday with increasing minutes
Turn on/off mobile hotspot on windows 10 via powershell
To turn on mobile hotspot:
PS C:\> [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile([Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()).StartTetheringAsync()↵
To turn off mobile hotspot:
PS C:\> [Windows.Networking.NetworkOperators.NetworkOperatorTetheringManager,Windows.Networking.NetworkOperators,ContentType=WindowsRuntime]::CreateFromConnectionProfile([Windows.Networking.Connectivity.NetworkInformation,Windows.Networking.Connectivity,ContentType=WindowsRuntime]::GetInternetConnectionProfile()).StopTetheringAsync()↵