PowerShell: Configure some basic Windows settings after a fresh installation

After installing Windows I always started configuring the same old basic settings:

  • move Desktop, Documents, Music, Videos, Pictures folders to D:\
  • configure some basic settings in Windows Explorer folder options (e.g. show hidden files, show file extensions)
  • remove Internet Explorer from taskbar and add PowerShell to it

Today I set up a few virtual machines and decided to finally script the above steps with PowerShell to save some time. Here’s the final script:
function changeUserFolders() { $profileRoot = "D:\Profil\Stefan\"; $folderSettings = @{ "Desktop" = "Desktop"; "Personal" = "Dokumente"; "My Music" = "Musik"; "My Pictures" = "Bilder"; "My Video" = "Videos"; "{374DE290-123F-4565-9164-39C4925E467B}" = "D:\Download"; }; $regkeys = @("HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"); foreach ($key in $folderSettings.keys) { $path = $folderSettings[$key]; if ($path -notmatch ":") { $path = [System.IO.Path]::Combine($profileRoot, $path); } if (!(test-path $path)) { mkdir $path; } foreach ($regkey in $regkeys) { set-ItemProperty -path $regkey -name $key $path; } } cd (gc env:\userprofile); foreach ($dir in @("Desktop", "Documents", "Downloads", "Music", "Pictures", "Videos")) { del $dir -force -recurse; } } function setExplorerSettings() { $settings = @{ "Hidden" = 1; "SuperHidden" = 1; "HideFileExt" = 0; "WebView" = 0; "SeparateProcess" = 1; "DontPrettyPath" = 1; "SharingWizardOn" = 0; "AlwaysShowMenus" = 1; "HideDrivesWithNoMedia" = 0; "NavPaneShowAllFolders" = 1; "NavPaneExpandToCurrentFolder" = 1; }; foreach ($key in $settings.keys) { Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -name $key -value $settings[$key]; } stop-process -processname explorer } function getVerbs($dir, $file) { $shell = new-object -com "Shell.Application"; $folder = $shell.Namespace($dir); $item = $folder.Parsename($file); return $item.Verbs(); } function executeVerb($dir, $file, $verb) { $verb = (getVerbs $dir $file) | ? {$_.Name -eq $verb}; if ($verb) { $verb.DoIt(); } } function pinToTaskbar($dir, $file) { executeVerb $dir $file "An Tas&kleiste anheften"; } function unpinToTaskbar($dir, $file) { executeVerb $dir $file "Von Tas&kleiste lösen"; } function createLinks() { $winDir = gc env:\SystemRoot; $progDir = gc "env:\ProgramFiles"; $prog86Dir = gc "env:\ProgramFiles(x86)"; $psDir = [System.IO.Path]::Combine($winDir, "system32\WindowsPowerShell\v1.0"); $ieDir = [System.IO.Path]::Combine($progDir, "Internet Explorer"); $ie86Dir = [System.IO.Path]::Combine($progDir, "Internet Explorer"); pinToTaskbar $psDir "powershell.exe"; unpinToTaskbar $ieDir "iexplore.exe"; unpinToTaskbar $ie86Dir "iexplore.exe"; } changeUserFolders; setExplorerSettings; createLinks;

Über Stefan

Polyglot Clean Code Developer

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax