This document provides a collection of PowerShell scripts for various software configurations.
# https://github.com/microsoft/winget-cli/issues/2489#issuecomment-1465198532
# Get the list of sources, skipping the first two lines
$sources = (winget source list | Select-Object -Skip 2) | ForEach-Object {
# Split each line into parts
$parts = $_ -split '\s+', 2
# Return the first part as the name of the source
return $parts[0]
}
# Remove the old default sources
foreach ($source in $sources) {
if ($source -ne "USTC") {
winget source remove -n $source
}
}
# Set the winget source to the USTC mirror
winget source add -n "USTC" -a "https://mirrors.ustc.edu.cn/winget-source/"
You can use PowerShell to change the homepage and search engine of Microsoft Edge to Google. However, please note that Microsoft Edge doesn't officially support changing these settings via PowerShell. The recommended way is to manually change them through the browser settings.
# Define the registry paths
$enrollmentRegistryPath = "HKLM:\SOFTWARE\Microsoft\Enrollments\FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
$accountRegistryPath = "HKLM:\SOFTWARE\Microsoft\Provisioning\OMADM\Accounts\FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"
$edgePolicyRegistryPath = "HKLM:\SOFTWARE\Policies\Microsoft\Edge"
# Check if the registry paths exist and create them if they don't
if (!(Test-Path $enrollmentRegistryPath)) {
New-Item -Path $enrollmentRegistryPath -Force | Out-Null
}
if (!(Test-Path $accountRegistryPath)) {
New-Item -Path $accountRegistryPath -Force | Out-Null
}
if (!(Test-Path $edgePolicyRegistryPath)) {
New-Item -Path $edgePolicyRegistryPath -Force | Out-Null
}
# Set the registry keys and values for the enrollment path
Set-ItemProperty -Path $enrollmentRegistryPath -Name "EnrollmentState" -Value 1
Set-ItemProperty -Path $enrollmentRegistryPath -Name "EnrollmentType" -Value 0
Set-ItemProperty -Path $enrollmentRegistryPath -Name "IsFederated" -Value 0
# Set the registry keys and values for the account path
Set-ItemProperty -Path $accountRegistryPath -Name "Flags" -Value 0xd6fb7f
Set-ItemProperty -Path $accountRegistryPath -Name "AcctUId" -Value "0x000000000000000000000000000000000000000000000000000000000000000000000000"
Set-ItemProperty -Path $accountRegistryPath -Name "RoamingCount" -Value 0
Set-ItemProperty -Path $accountRegistryPath -Name "SslClientCertReference" -Value "MY;User;0000000000000000000000000000000000000000"
Set-ItemProperty -Path $accountRegistryPath -Name "ProtoVer" -Value "1.2"
# Set the registry keys and values for the Edge policy path
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "AddressBarMicrosoftSearchInBingProviderEnabled" -Value 0
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "AdsSettingForIntrusiveAdsSites" -Value 2
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "ClearCachedImagesAndFilesOnExit" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "ConfigureDoNotTrack" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "DefaultSearchProviderEnabled" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "DefaultSearchProviderSearchURL" -Value "{google:baseURL}search?q=%s&{google:RLZ}{google:originalQueryForSuggestion}{google:assistedQueryStats}{google:searchFieldtrialParameter}{google:iOSSearchLanguage}{google:searchClient}{google:sourceId}{google:contextualSearchVersion}ie={inputEncoding}"
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "DefaultSearchProviderName" -Value "Google"
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "DefaultSearchProviderSuggestURL" -Value "{google:baseURL}complete/search?output=chrome&q={searchTerms}"
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "SmartScreenEnabled" -Value 0
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "SmartScreenPuaEnabled" -Value 0
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "HomepageLocation" -Value "about:blank"
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "HomepageIsNewTabPage" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "NewTabPageLocation" -Value "about:blank"
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "NewTabPageSetFeedType" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "RestoreOnStartup" -Value 5
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "ShowHomeButton" -Value 1
Set-ItemProperty -Path $edgePolicyRegistryPath -Name "EdgeShoppingAssistantEnabled" -Value 0
The following PowerShell script is used to add bookmarks to Microsoft Edge by directly modifying the Bookmarks
file.
# https://stackoverflow.com/a/75644768
# Define the path to the Bookmarks file
$bmpath="$env:APPDATA\..\Local\Microsoft\Edge\User Data\Default\Bookmarks"
# Check if the parent directory exists
if (!(Test-Path (Split-Path $bmpath -Parent))) {
Write-Error "The parent directory does not exist: $(Split-Path $bmpath -Parent)"
return
}
# Check if the Bookmarks file exists, if not, create it
if (!(Test-Path $bmpath)) {
$initialContent = @{
"roots" = @{
"bookmark_bar" = @{
"children" = @()
"date_added" = "13255192080000000"
"guid" = [guid]::NewGuid().ToString()
"id" = "1"
"name" = "Bookmarks bar"
"type" = "folder"
}
}
"version" = 1
} | ConvertTo-Json -Depth 5
New-Item -ItemType File -Path $bmpath -Force | Out-Null
Set-Content -Path $bmpath -Value $initialContent
}
# New bookmark
$newbk = [pscustomobject][ordered]@{guid=New-Guid; name="Test Bookmark"; show_icon=$false;
source="user_copy"; type="url";
url="https://www.google.com/ncr"}
# New bookmarklet
$url = @'
javascript: (() => {
alert('Hello, World!');
})();
'@
$newbkjs = [pscustomobject][ordered]@{guid=New-Guid; name="Bookmarklet";
show_icon=$false; source="user_copy"; type="url"; url=$url}
# Read the existing Bookmarks file
$bk=Get-Content $bmpath | ConvertFrom-Json
# Add the new bookmarks to the bookmark bar
$bk.roots.bookmark_bar.children += $newbk
$bk.roots.bookmark_bar.children += $newbkjs
$bk.psobject.Properties.Remove('checksum')
# Write the modified bookmarks back to the Bookmarks file
$bk | ConvertTo-Json -Depth 4 | Set-Content $bmpath
# Close all running instances of Microsoft Edge
Get-Process -Name msedge | Stop-Process -Force
# Wait for a few seconds to make sure all instances are closed
Start-Sleep -Seconds 5
# Start a new instance of Microsoft Edge
Start-Process -FilePath "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
This script first checks if the Bookmarks
file exists. If the file does not exist, it creates a new file with a default structure. Then it reads the existing Bookmarks
file and converts its JSON content to a PowerShell object.
The script then defines two new bookmarks: a regular bookmark for "https://www.google.com" and a JavaScript bookmarklet that shows an alert when clicked. These bookmarks are added to the bookmark bar.
Finally, the script converts the modified bookmarks back to JSON and writes them to the Bookmarks
file.
See https://developer.aliyun.com/article/453857
Change the proxy to mirrors.tuna.tsinghua.edu.cn:80
or mirrors.aliyun.com:80