I'd like to automate preferences on a Windows machine for the date and time formats used throughout the OS.
What PowerShell commands can I use to automate this task of changing values?
short date format
short and long time format
These options are buried deep in Control Panel.
I believe the settings you are looking for are located in:
HKEY_CURRENT_USER\Control Panel\International\sLongDate
HKEY_CURRENT_USER\Control Panel\International\sShortDate
HKEY_CURRENT_USER\Control Panel\International\sTimeFormat
HKEY_CURRENT_USER\Control Panel\International\sYearMonth
So,
Set-ItemProperty -Path "HKCU:\Control Panel\International"
-name sLongDate -value "<Whatever format you'd like>"
Based on p.campbells's answer, but with all properties:
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sCountry -Value "Germany";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sLongDate -Value "dddd, d. MMMM yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sShortDate -Value "dd.MM.yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sShortTime -Value "HH:mm";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sTimeFormat -Value "HH:mm:ss";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sYearMonth -Value "MMMM yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name iFirstDayOfWeek -Value 0;
Full example based on p.campbell's answer:
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sCountry -Value "Germany";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sLongDate -Value "dddd, d. MMMM yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sShortDate -Value "dd.MM.yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sShortTime -Value "HH:mm";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sTimeFormat -Value "HH:mm:ss";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sYearMonth -Value "MMMM yyyy";
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name iFirstDayOfWeek -Value 0;
To set the settings of the default user instead of the system, use HKU:\.DEFAULT\Control Panel\International. For a specific user use HKU:\(UserSID)\Control Panel\International.
To get the SID of the current user, use
([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
If you don't have HKU: mapped, use
New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS
Related
(How) can I pipe this (without using the variables)?
$regPath="HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize";
$regProp="AppsUseLightTheme";
$toggledDarkMode = (Get-ItemProperty $regPath $regProp).$regProp -bxor 1
Set-ItemProperty $regPath $regProp $toggledDarkMode;
Something like:
Get-ItemProperty ... | Set-ItemProperty -value ($_ -bxor 1) ...
Use ForEach-Object to wrap the call to Set-ItemProperty:
Get-ItemPropertyValue -Path $regPath -Name $regProp |ForEach-Object {
Set-ItemProperty -Path $regPath -Name $regProp -Value ($_ -bxor 1)
}
I am referring to the PowerShell script to pause windows 10 updates up to 35 days here.
It states to pause windows 10 updates via powershell write this:
$pause = (Get-Date).AddDays(35)
$pause = $pause.ToUniversalTime().ToString( "yyyy-MM-ddTHH:mm:ssZ" )
$pause_start = (Get-Date)
$pause_start = $pause_start.ToUniversalTime().ToString( "yyyy-MM-ddTHH:mm:ssZ" )
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseUpdatesExpiryTime' -Value $pause
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseFeatureUpdatesStartTime' -Value $pause_start
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseFeatureUpdatesEndTime' -Value $pause
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseQualityUpdatesStartTime' -Value $pause_start
Set-itemproperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseQualityUpdatesEndTime' -Value $pause
This doesn't work in Windows 11. Does anybody knows what doesn't work and why?
Ok so I went to check what happens when I pause the updates manually. Actually there Is one more key which is added, with the name "PauseUpdatesStartTime".
So I tried the powershell script with this key and it worked. Now the corrected script looks like:
$pause = (Get-Date).AddDays(35)
$pause = $pause.ToUniversalTime().ToString( "yyyy-MM-ddTHH:mm:ssZ" )
$pause_start = (Get-Date)
$pause_start = $pause_start.ToUniversalTime().ToString( "yyyy-MM-ddTHH:mm:ssZ" )
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseUpdatesExpiryTime' -Value $pause
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseFeatureUpdatesStartTime' -Value $pause_start
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseFeatureUpdatesEndTime' -Value $pause
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseQualityUpdatesStartTime' -Value $pause_start
Set-itemproperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseQualityUpdatesEndTime' -Value $pause
Set-itemproperty -Path 'HKLM:\SOFTWARE\Microsoft\WindowsUpdate\UX\Settings' -Name 'PauseUpdatesStartTime' -Value $pause_start
New-Item -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU' -Force
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU' -Name 'NoAutoUpdate' -PropertyType DWORD -Value 1
To execute the ps script via cmd, I jut need to write this:
> powershell -executionpolicy unrestricted C:\path_to_my_script\pausewin11upd.ps1
My code:
try {
if(!(Test-Path -Path $registryPath -Value $Name)) {
New-ItemProperty -Name test -Path HKLM:\Software\WOW6432Node\Mozilla -PropertyType DWord -Value 2
}
}
catch {
Set-ItemProperty -Path $registryPath -Name $Name -Value $value
}
My Problem: the result comes out as string.
I have tried changing -PropertyType to -Type.
I have tried changing the capitilation of the word DWord.
Any help would be greatly appreciated.
As the docs say:
You also use Set-ItemProperty to create and change registry values and data. For example, you can add a new registry entry to a key and establish or change its value.
With Set-ItemProperty it is also possible to change the type of registry entry from say a REG_SZ (string) value to DWord, so basically all you need is:
$registryPath = 'HKLM:\Software\WOW6432Node\Mozilla'
$propName = 'test'
Set-ItemProperty -Path $registryPath -Name $propName -Value 2 -Type DWord
Of course, if you do not have permissions to create or alter something in the HKEY_LOCAL_MACHINE registry path, you will rceive an error.
I'm trying to optimize IE11 for our application per user in case a GP is not allowed to be used. I want to optimize these settings with a single script so that it doesn't take me a lot of times each time i get the request.
I tried to create a powershell script that first tests the path of the registry key.
After that it should provide a message to either cancel or continue and then it should change the values.
So far the "test-path" part works, but realy changing the values is not.
$RegPaths =
'HKCU:\Software\Microsoft\Internet Explorer\BrowserEmulation',
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap',
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1',
'HKCU:\Software\Microsoft\Internet Explorer\New Windows'
#Test-path $RegPaths
$RegEdit =
{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet Explorer\BrowserEmulation' -name IntranetCompatibilityMode -value 0},
{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet Explorer\BrowserEmulation' -name MSCompatibilityModegpupd -value 0},
{Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -name AutoDetect -value 1},
{Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1' -name 1001 -value 0},
{Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1' -name 1004 -value 0},
{Set-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1' -name 1201 -value 0},
{Set-ItemProperty -path 'HKCU:\Software\Microsoft\Internet Explorer\New Windows' -name PopupMgr -value 0}
#If (Test-path $RegPaths = True)
#{write-host "Registry paths exist, continueing improving IE settings for ISCV client"}
#Foreach ($RegEdit) {if (Test-path $RegPaths -eq $True) {continue}}
#Else {write-host "Register path invalid or missing, canceling changes"}
If (( Test-Path $RegPaths) -eq $True)
{
{write-host "Registry paths exist, continueing improving IE settings for ISCV client"}
#{ForEach-object -process $RegEdit}
Foreach ($RegEdit in $RegEdit)
{start-job $RegEdit}
}
Else
{Write-Host " Path missing or invalid, cancel script"}
The -process, or the start-job is not changing the values from 0 to 1 for example, which is expected.
Thanks!
Update
Thanks for the advise so far. I was trying and noticed that only the "write-host" line and the first set-itemproperty line are actually working, after is nothing happening, any idea how or why?
{write-host "Registry paths exist, continueing improving IE settings for
client"}
&{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet
Explorer\BrowserEmulation' -name IntranetCompatibilityMode -value 0}
&{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet
Explorer\BrowserEmulation' -name MSCompatibilityModegpupd -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap'
-name AutoDetect -value 1}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1001 -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1004 -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1201 -value 0}
&{Set-ItemProperty -path 'HKCU:\Software\Microsoft\Internet Explorer\New
Windows' -name PopupMgr -value 0}
Update the above update is working, i didn't look good. Thanks!
this code is working ok for me now:
{write-host "Registry paths exist, continueing improving IE settings for
client"}
&{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet
Explorer\BrowserEmulation' -name IntranetCompatibilityMode -value 0}
&{Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Internet
Explorer\BrowserEmulation' -name MSCompatibilityModegpupd -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap'
-name AutoDetect -value 1}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1001 -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1004 -value 0}
&{Set-ItemProperty -Path
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1'
-name 1201 -value 0}
&{Set-ItemProperty -path 'HKCU:\Software\Microsoft\Internet Explorer\New
Windows' -name PopupMgr -value 0}
I am currently creating a powershell script in which the technicians can run to help apply various registry edits to create certain PCs which have automatic logins. However, whenever I run my script the powershell has no issue when changing values with pre-existing keys, yet it will not create keys when using the "new-item" command. I was wondering whether anyone would have any idea as to why this would not create the registry key given that I receive no errors when run.
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" –Name AutoAdminLogon -Value 1
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultUserName -Value domain\TEST
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name DefaultPassword -Value TEST123
Test-Path –Path "HKLM:\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon\ForceAutoLogon"
if ( -Not (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"))
{
New-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" -Name ForceAutoLogon -Value 1
}
Test-Path is not designed for registy values. What you can do it use a Try/Catch block. You also need to Get/Set the itemPropery.
$Path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Try {
Get-ItemProperty -Path $Path | Select-Object -ExpandProperty ForceAutoLogon -ErrorAction Stop | Out-Null
Set-ItemProperty -Path $Path -Name ForceAutoLogon -Value 1
} Catch {
New-ItemProperty -Path $Path -Name ForceAutoLogon -Value 1
}
If the Get-ItemProperty fails the the key must not exist. Then we can create it! If Get-ItemProperty succeeds then we can ensure the value is set properly. I might be using the registry keywords wrong but let the code speak for itself.