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:

  1. function changeUserFolders()
  2. {
  3.     $profileRoot = "D:\Profil\Stefan\";
  4.  
  5.     $folderSettings = @{
  6.         "Desktop" = "Desktop";
  7.         "Personal" = "Dokumente";
  8.         "My Music" = "Musik";
  9.         "My Pictures" = "Bilder";
  10.         "My Video" = "Videos";
  11.         "{374DE290-123F-4565-9164-39C4925E467B}" = "D:\Download";
  12.     };
  13.  
  14.     $regkeys = @("HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders", "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
  15.  
  16.     foreach ($key in $folderSettings.keys)
  17.     {
  18.         $path = $folderSettings[$key];
  19.         if ($path -notmatch ":")
  20.         {
  21.             $path = [System.IO.Path]::Combine($profileRoot, $path);
  22.         }
  23.         if (!(test-path $path))
  24.         {
  25.             mkdir $path;
  26.         }
  27.         foreach ($regkey in $regkeys)
  28.         {
  29.             set-ItemProperty -path $regkey -name $key $path;
  30.         }
  31.     }
  32.  
  33.     cd (gc env:\userprofile);
  34.     foreach ($dir in @("Desktop", "Documents", "Downloads", "Music", "Pictures", "Videos"))
  35.     {
  36.         del $dir -force -recurse;
  37.     }
  38. }
  39.  
  40. function setExplorerSettings()
  41. {
  42.     $settings = @{
  43.         "Hidden" = 1;
  44.         "SuperHidden" = 1;
  45.         "HideFileExt" = 0;
  46.         "WebView" = 0;
  47.         "SeparateProcess" = 1;
  48.         "DontPrettyPath" = 1;
  49.         "SharingWizardOn" = 0;
  50.         "AlwaysShowMenus" = 1;
  51.         "HideDrivesWithNoMedia" = 0;
  52.         "NavPaneShowAllFolders" = 1;
  53.         "NavPaneExpandToCurrentFolder" = 1;
  54.     };
  55.  
  56.     foreach ($key in $settings.keys)
  57.     {
  58.         Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -name $key -value $settings[$key];
  59.     }
  60.     stop-process processname explorer
  61. }
  62.  
  63. function getVerbs($dir, $file)
  64. {
  65.     $shell = new-object com "Shell.Application";
  66.     $folder = $shell.Namespace($dir);
  67.     $item = $folder.Parsename($file);
  68.     return $item.Verbs();
  69. }
  70.  
  71. function executeVerb($dir, $file, $verb)
  72. {
  73.     $verb = (getVerbs $dir $file) | ? {$_.Name -eq $verb};
  74.     if ($verb)
  75.     {
  76.         $verb.DoIt();
  77.     }
  78. }
  79.  
  80. function pinToTaskbar($dir, $file)
  81. {
  82.     executeVerb $dir $file "An Tas&kleiste anheften";
  83. }
  84.  
  85. function unpinToTaskbar($dir, $file)
  86. {
  87.     executeVerb $dir $file "Von Tas&kleiste lösen";
  88. }
  89.  
  90. function createLinks()
  91. {
  92.     $winDir = gc env:\SystemRoot;
  93.     $progDir = gc "env:\ProgramFiles";
  94.     $prog86Dir = gc "env:\ProgramFiles(x86)";
  95.    
  96.     $psDir = [System.IO.Path]::Combine($winDir, "system32\WindowsPowerShell\v1.0");
  97.     $ieDir = [System.IO.Path]::Combine($progDir, "Internet Explorer");
  98.     $ie86Dir = [System.IO.Path]::Combine($progDir, "Internet Explorer");
  99.    
  100.     pinToTaskbar $psDir "powershell.exe";
  101.     unpinToTaskbar $ieDir "iexplore.exe";
  102.     unpinToTaskbar $ie86Dir "iexplore.exe";
  103. }
  104.  
  105. changeUserFolders;
  106. setExplorerSettings;
  107. createLinks;

Über uns 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