iax

Set-FolderIcon

iax
Mar 7th, 2018
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .SYNOPSIS
  3. This function sets a folder icon on specified folder.
  4. .DESCRIPTION
  5. This function sets a folder icon on specified folder. Needs the path to the icon file to be used and the path to the folder the icon is to be applied to. This function will create two files in the destination path, both set as Hidden files. DESKTOP.INI and FOLDER.ICO
  6. .EXAMPLE
  7. Set-FolderIcon -Icon "C:\Users\Mark\Downloads\Radvisual-Holographic-Folder.ico" -Path "C:\Users\Mark"
  8. Changes the default folder icon to the custom one I donwloaded from Google Images.
  9. .EXAMPLE
  10. Set-FolderIcon -Icon "C:\Users\Mark\Downloads\wii_folder.ico" -Path "\\FAMILY\Media\Wii"
  11. Changes the default folder icon to custom one for a UNC Path.
  12. .EXAMPLE
  13. Set-FolderIcon -Icon "C:\Users\Mark\Downloads\Radvisual-Holographic-Folder.ico" -Path "C:\Test" -Recurse
  14. Changes the default folder icon to custom one for all folders in specified folder and that folder itself.
  15. .NOTES
  16. Created by Mark Ince on May 4th, 2014. Contact me at [email protected] if you have any questions.
  17. #>
  18. function Set-FolderIcon
  19. {
  20.     [CmdletBinding()]
  21.     param
  22.     (    
  23.         [Parameter(Mandatory=$True,
  24.         Position=0)]
  25.         [string[]]$Icon,
  26.         [Parameter(Mandatory=$True,
  27.         Position=1)]
  28.         [string]$Path,
  29.         [Parameter(Mandatory=$False)]
  30.         [switch]
  31.         $Recurse    
  32.     )
  33.     BEGIN
  34.     {
  35.         $originallocale = $PWD
  36.         #Creating content of the DESKTOP.INI file.
  37.         $ini = '[.ShellClassInfo]
  38.                IconFile=folder.ico
  39.                IconIndex=0
  40.                ConfirmFileOp=0'
  41.         Set-Location $Path
  42.         Set-Location ..    
  43.         Get-ChildItem | Where-Object {$_.FullName -eq "$Path"} | ForEach {$_.Attributes = 'Directory, System'}
  44.     }    
  45.     PROCESS
  46.     {
  47.         $ini | Out-File $Path\DESKTOP.INI
  48.         If ($Recurse -eq $True)
  49.         {
  50.             Copy-Item -Path $Icon -Destination $Path\FOLDER.ICO    
  51.             $recursepath = Get-ChildItem $Path -r | Where-Object {$_.Attributes -match "Directory"}
  52.             ForEach ($folder in $recursepath)
  53.             {
  54.                 Set-FolderIcon -Icon $Icon -Path $folder.FullName
  55.             }
  56.          
  57.         }
  58.         else
  59.         {
  60.             Copy-Item -Path $Icon -Destination $Path\FOLDER.ICO
  61.         }    
  62.     }    
  63.     END
  64.     {
  65.         $inifile = Get-Item $Path\DESKTOP.INI
  66.         $inifile.Attributes = 'Hidden'
  67.         $icofile = Get-Item $Path\FOLDER.ICO
  68.         $icofile.Attributes = 'Hidden'
  69.         Set-Location $originallocale        
  70.     }
  71. }
  72. <#
  73.  
  74. #>
  75. function Remove-SetIcon
  76. {
  77.     [CmdletBinding()]
  78.     param
  79.     (    
  80.         [Parameter(Mandatory=$True,
  81.         Position=0)]
  82.         [string]$Path
  83.     )
  84.     BEGIN
  85.     {
  86.         $originallocale = $PWD
  87.         $iconfiles = Get-ChildItem $Path -Recurse -Force | Where-Object {$_.Name -like "FOLDER.ICO"}
  88.         $iconfiles = $iconfiles.FullName
  89.         $inifiles = Get-ChildItem $Path -Recurse -Force | where-Object {$_.Name -like "DESKTOP.INI"}
  90.         $inifiles = $inifiles.FullName
  91.     }
  92.     PROCESS
  93.     {
  94.         Remove-Item $iconfiles -Force
  95.         Remove-Item $inifiles -Force
  96.         Set-Location $Path
  97.         Set-Location ..
  98.         Get-ChildItem | Where-Object {$_.FullName -eq "$Path"} | ForEach {$_.Attributes = 'Directory'}    
  99.     }
  100.     END
  101.     {
  102.         Set-Location $originallocale
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment