#Set-SharedFolderIcons #This script enumerates shares on system and sets the folder icon #to a selected value. #At end of file are optional edits, including test mode. #Tested on Server 2012 R2 #Alan at Akaplan dot com #1/3/2017 Function Set-SharedFolderIcons{ $iniContent = @" [.ShellClassInfo] IconResource=$dllFile,$IconIndex [ViewState] Mode= Vid= FolderType=Generic "@ if ($bConfirmIconChoice){ $bContinue = Check-IconChoice $dllFile $iconIndex if ($bContinue -eq $false) { Write-Warning "Edit script to select a different Icon Choice" Exit } } $Title = "Change icon for these selected folders" if ($btest){$Title += ' [Test mode]'} #Get list of ordinary shares $Shares = Get-WmiObject -Class Win32_Share | where {($_.type -eq 0) -and ($_.name -ne 'print$') -and ($_.Path -notmatch [regex]::Escape($env:windir))} | select Name, Path | Out-GridView -Title $Title -PassThru if ($shares){ $shares |Foreach { $sharePath = [string]$_.Path $NewINI = "$SharePath\desktop.ini" if ($bTest){ write "What if: `"Creating $NewIni`"" }ELSE{ Write "Setting Icon for $sharepath" if (Test-Path $NewINI) {Remove-Item $NewINI -Force} $(New-item -itemtype file -path $NewINI -force).attributes='hidden','system' Set-Content -Value $iniContent -Path $NewINI get-item $sharePath | Set-ItemProperty -name attributes -value "System" } } }ELSE{Exit} Write "Done" } Function Check-IconChoice ( $SourceBINFilePath, $IconIndexNo){ #Icon Extractor #https://social.technet.microsoft.com/Forums/scriptcenter/en-US/16444c7a-ad61-44a7-8c6f-b8d619381a27/using-icons-in-powershell-scripts?forum=winserverpowershell $code = @" using System; using System.Drawing; using System.Runtime.InteropServices; namespace System { public class IconExtractor { public static Icon Extract(string file, int number, bool largeIcon) { IntPtr large; IntPtr small; ExtractIconEx(file, number, out large, out small, 1); try { return Icon.FromHandle(largeIcon ? large : small); } catch { return null; } } [DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)] private static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); } } "@ Add-type -assemblyname 'Microsoft.VisualBasic' Add-Type -TypeDefinition $code -ReferencedAssemblies System.Drawing add-type -AssemblyName 'system.windows.forms' $objForm = New-Object System.Windows.Forms.Form $ico = [System.IconExtractor]::Extract($SourceBINFilePath, $IconIndexNo, $true) $objForm.Icon = $ico #Much of basic form stuff is from Scripting Guys posts #https://blogs.technet.microsoft.com/heyscriptingguy/ $objForm.KeyPreview = $True $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objTextBox.Text;$objForm.Close()}}) $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}}) $OKButton = New-Object System.Windows.Forms.Button $OKButton.Location = New-Object System.Drawing.Size(25,120) $OKButton.Size = New-Object System.Drawing.Size(75,23) $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::Yes $OKButton.Text = "Yes" $OKButton.Add_Click({ $objForm.Close() }) $objForm.Controls.Add($OKButton) $CancelButton = New-Object System.Windows.Forms.Button $CancelButton.Location = New-Object System.Drawing.Size(100,120) $CancelButton.Size = New-Object System.Drawing.Size(75,23) $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::No $CancelButton.Text = "No" $CancelButton.Add_Click({ $objForm.Close() }) $objForm.Controls.Add($CancelButton) $objLabel = New-Object System.Windows.Forms.Label $objLabel.Location = New-Object System.Drawing.Size(20,60) $objLabel.Size = New-Object System.Drawing.Size(280,20) $objLabel.Text = "Is this the Icon you want?" $objForm.Controls.Add($objLabel) $objForm.Topmost = $True $objform.ControlBox = $false $objForm.StartPosition = "CenterScreen" $objForm.AutoSize = $true $img = $ico $objForm.Text = "Confirm Folder Icon" $objForm.Width = ($img.Width) * 5 $objForm.Height = ($img.Height) * 5 $pictureBox = new-object Windows.Forms.PictureBox $pictureBox.Width = ($img.Width) * 5 $pictureBox.Height = ($img.Height) * 5 $pictureBox.Image = $img $objForm.controls.add($pictureBox) $objForm.Add_Shown( { $objForm.Activate() } ) $retval = $objForm.ShowDialog() if($retval -eq ([System.Windows.Forms.DialogResult]::Yes)) {$True}ELSE{$false} } ################## Script Begins ################### #You can edit these values $dllFile = "c:\windows\system32\imageres.dll" $iconIndex = 169 $btest = $false #if False, will not be prompted for folder choice $bConfirmIconChoice = $true #End Edits Set-SharedFolderIcons