I have this powershell script for Windows which would do a few changes for all new users that log into the computer for the first time, It should only run once, I have tried Taskscheduler, Runonce registry key and even modifying the Default user to run this script once but nothing so far. Would you guys help me or reference a guide to get this done? Please see the script below.
Powershell:
# Define the path to the flag file
$flagFilePath = "C:\path\to\flag.txt"
# Check if the flag file exists
if (-not (Test-Path $flagFilePath)) {
# Additional check for new user condition
if (Test-Path "C:\path\to\new_user_setting.txt") {
# Your script logic goes here
# Set start menu to the left
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
If (!(Test-Path $regPath)) {
New-Item -Path $regPath -Force
}
$alignment = 0
New-ItemProperty -Path $regPath -Name TaskBarAl -Value $alignment -PropertyType DWORD -Force
# Disable search box
Set-ItemProperty -Path “HKCU:\Software\Microsoft\Windows\CurrentVersion\Search” -Name “SearchboxTaskbarMode” -Value 0
#Turn Off Copilot
reg add HKCU\Software\Policies\Microsoft\Windows\WindowsCopilot /v "TurnOffWindowsCopilot" /t REG_DWORD /f /d 1
#Unpin Microsoft Edge From taskbar or any other app
function Unpin-App([string]$appname) {
((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() |
?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Unpin from taskbar'} | %{$_.DoIt()}
}
Unpin-App("Microsoft Edge")
#Remove task view from taskbar
New-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "ShowTaskViewButton" -Value 0 -Force
# Restart explorer refresh changes
Stop-Process -Name Explorer
Start-Process explorer
Write-Host “Executed”
# The scripts below require elevated permissions, otherwise it throws an error
#Removes personalized News
$AppxRemoval = Get-AppxProvisionedPackage -Online | Where-Object {$_.PackageName -like “WebExperience*”}
ForEach ($App in $AppxRemoval) {
Remove-AppxProvisionedPackage -Online -Package $App.PackageName }
# Remove widgets
Get-AppxPackage -AllUsers | Where-Object {$_.Name -like “*WebExperience*”} |
Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue
# Create the flag file to indicate the script has been executed
New-Item -ItemType File -Path $flagFilePath -Force
Write-Host "Script executed successfully for a new user."
} else {
Write-Host "New user condition not met. Script not executed."
}
} else {
Write-Host "Script has already been executed once."
}
- Windows Build/Version
- Windows 11 Pro 23H2 Build 22631.3155
My Computer
System One
-
- OS
- Windows10/11
- Computer type
- PC/Desktop
- Manufacturer/Model
- Dell
- CPU
- i9-12900k
- Motherboard
- Asus Z960
- Memory
- 32 GB
- Graphics Card(s)
- Nvidia RTX 4070
- Sound Card
- N/A
- Monitor(s) Displays
- LG UHD ULTRAWIDE 39"
- Screen Resolution
- 3440p x 1440p
- Hard Drives
- Nvme SSD 1TB
- PSU
- 850 Watts
- Case
- Y60
- Cooling
- CORSAIR iCUE H150i
- Keyboard
- NuPhy Air96 V2
- Mouse
- Dell Wireless
- Internet Speed
- 1 GB
- Browser
- Chrome/Edge
- Antivirus
- Windows Defender