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
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 have created a powershell script that enables basic authentication, I needed this to allow the winrm to work when running some of our older powershell scripts.
What I need to do now is be able to call this script as a function with either a true false argument. e.g. disable or enable basic authentication.
How can I wrap this code into a function so that I can call it from other powershell scripts?
SO if I send a command to this script e.g.
basicauth($true) - it will run the script as is
basicauth($false - would disable basic authentication
I can create the alternate if else statement for when the true of false is sent to this, but not sure how I can wrap the whole script into a function.
Apologies for the novice status with powershell, it took me awhile to get this script working the way it is.
param([switch]$Elevated)
# Get variables
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
$key1 = "AllowDigest"
$key2 = "AllowUnencryptedTraffic"
$key3 = "AllowBasic"
$off = "00000000"
$on = "00000001"
# enables admin privileges
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
if ((Test-Admin) -eq $false) {
if ($elevated)
{
'tried to elevate, did not work, aborting...'
}
else {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
}
exit
}
# checks if the registry path is available, before adding the registry key values
If (!(Test-Path $registryPath))
{
New-Item -Path $registryPath -Force | out-Null
New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
#'registry key did not exist'
exit
}
Else
{
New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
#'registry key exists'
exit
}
NOTE: Now I know that the Else statement should have the values as: Set-ItemProperty although if I change the code to have Set-ItemProperty the script no longer works, only way I have this working is to have it as: New-ItemProperty. Doesn't really make sense but it works.
Ideally it would be better to just update the current powershell scripts to use modern authentication, but there is 100's of them so not really a viable option for me.
Any assistance would be greatly appreciated.
If you wrap the entire function in an if statement like:
param([switch]$Elevated)
if($elevated) {
...script code here
}
Then you can call the function with that parameter like . "scriptname.ps1" -Elevated to execute what is inside the scriptblock. Instead just calling . "scriptname.ps1" without the -Elevated parameter will not do anything because you'll hit your if statement:
if ($elevated) {
and elevated doesn't exist which means nothing inside the scriptblock executes.
I don't see the purpose of doing this in your case because if you already have logic to decide whether or not to pass in true or false, why not just use that logic to decide whether or not to call the script at all? My guess is that you don't actually mean that you only want to execute the entire script if a user is 'elevated' but rather check if they can be elevated in the script and then do something else.
In that case you should take a look at Advanced PowerShell Functions. You could do something like this:
# Get variables
$registryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WinRM\Client"
$key1 = "AllowDigest"
$key2 = "AllowUnencryptedTraffic"
$key3 = "AllowBasic"
$off = "00000000"
$on = "00000001"
# enables admin privileges
function Test-Admin {
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$isAdmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
return $isAdmin
}
function Do-TheRestOfTheThings {
[CmdletBinding()]
param()
Get-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
If (!(Test-Path $registryPath)) {
New-Item -Path $registryPath -Force | out-Null
New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
#'registry key did not exist'
exit
}
Else {
New-ItemProperty -Path $registryPath -Name $key1 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key2 -Value $off -PropertyType DWORD -Force | Out-Null
New-ItemProperty -Path $registryPath -Name $key3 -Value $on -PropertyType DWORD -Force | Out-Null
#'registry key exists'
}
}
if(Test-Admin) {
Do-TheRestOfTheThings
}
else {
'tried to elevate, did not work, aborting...'
}
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 started creating this script and I got stuck at one thing that I want to accomplish. This script runs fine. But what I really want to do is when this script is run more than once, I want to change the value to the number of times this script was use. How do I accomplish this?
$RegistryPath = "HKLM:\SOFTWARE\"
$NewRegKey = "SCCM"
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName = "Attempts"
$Value = "1"
New-Item -Path $RegistryPath -Name $NewRegKey -Force
New-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value $Value -PropertyType DWORD -Force -ErrorAction SilentlyContinue | Out-Null
NEW script:
(Working)
#This is creating the SCCM Hive and add the key W10_IPU_Attempts
If (Get-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -ErrorAction SilentlyContinue) {
} Else {
$RegistryPath = "HKLM:\SOFTWARE\"
$NewRegKey = "SCCM"
New-Item -Path $RegistryPath -Name $NewRegKey -Force
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName = "W10_IPU_Attempts"
$Value = "0"
New-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value $Value -PropertyType DWORD -Force | Out-Null
}
#Set value in increment of 1 each time this script is ran!
$i=
$Value =(Get-Itemproperty 'HKLM:\SOFTWARE\SCCM').W10_IPU_Attempts
If ($i -eq $value){
$i++
Set-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -Name W10_IPU_Attempts -Value $i
}
else {
$i++
Set-ItemProperty -Path 'HKLM:\SOFTWARE\SCCM' -Name W10_IPU_Attempts -Value $i
}
I suggest to use splatting with your bunch of parameters.
Otherwise do as suggested, (check) read current value set incremented value,
if not present, create the key
## Q:\Test\2019\05\17\SO_56190969.ps1
$RegistryPath = "HKLM:\SOFTWARE\"
$NewRegKey = "SCCM"
$NewRegKeyPath = "HKLM:\SOFTWARE\SCCM"
$KeyName = "Attempts"
if ($Value=[Int](Get-ItemPropertyValue $NewRegKeyPath -Name $KeyName -EA 0)){
Set-ItemProperty -Path $NewRegKeyPath -Name $KeyName -Value (++$Value) -Type DWord
} else {
New-Item -Path $RegistryPath -Name $NewRegKey -Force | Out-Null
$params = #{
Path = $NewRegKeyPath
Name = $KeyName
Value = 1
PropertyType= 'DWORD'
Force = $True
ErrorAction = 'SilentlyContinue'
}
New-ItemProperty #params | Out-Null
}
"Key : {0}`nKeyname: {1}`nValue : {2}" -f $NewRegKeyPath,$KeyName,
(Get-ItemPropertyValue $NewRegKeyPath -Name $KeyName)
#
Two sample runs:
> Q:\Test\2019\05\17\SO_56190969.ps1
Key : HKLM:\SOFTWARE\SCCM
Keyname: Attempts
Value : 1
> Q:\Test\2019\05\17\SO_56190969.ps1
Key : HKLM:\SOFTWARE\SCCM
Keyname: Attempts
Value : 2
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