Advertisement
Lee_Dailey

function Get-SpecialFolderInfo - 2018.01.20.15.54.11

Jan 20th, 2018
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function Get-SpecialFolderInfo
  2.         {
  3.         #region >> CBH [Comment Based Help]
  4.  
  5.         <#
  6.         .Synopsis
  7.             Short description
  8.  
  9.         .DESCRIPTION
  10.             Long description
  11.  
  12.         .EXAMPLE
  13.             Example of how to use this cmdlet
  14.  
  15.         .EXAMPLE
  16.             Another example of how to use this cmdlet
  17.  
  18.         .INPUTS
  19.             Inputs to this cmdlet (if any)
  20.  
  21.         .OUTPUTS
  22.             Output from this cmdlet (if any)
  23.  
  24.         .NOTES
  25.             Version = 2018.01.20.15.54.11
  26.         #>
  27.  
  28.         #endregion >> CBH [Comment Based Help]
  29.  
  30.         #region >> Parameters
  31.         [CmdletBinding ()]
  32.  
  33.         Param
  34.             (
  35.             [Parameter (
  36.                 Position = 0)]
  37.                 [ValidateNotNullOrEmpty ()]
  38.                 [string[]]
  39.                 $SpecialFolderName = '*',
  40.  
  41.             [Parameter (
  42.                 Position = 1)]
  43.                 [ValidateSet ('All', 'NameOnly', 'PathOnly')]
  44.                 [string]
  45.                 $WhatToReturn = 'All'
  46.  
  47.  
  48.             )
  49.         #endregion >> Parameters
  50.  
  51.         #region >> begin
  52.         begin
  53.             {
  54.             Write-Verbose 'Building list of Special Folder names ...'
  55.             $Full_SFN_List = [enum]::GetNames('System.Environment+SpecialFolder') |
  56.                 Sort-Object
  57.             Write-Verbose ('    {0} Special Folder names found.' -f $Full_SFN_List.Count)
  58.  
  59.             $NoPathFound = '--- No Path Found --'
  60.             }
  61.         #endregion >> begin
  62.  
  63.         #region >> process
  64.         process
  65.             {
  66.             Write-Verbose 'Removing unwanted Special Folder names ...'
  67.             $SFN_WorkingList = $Full_SFN_List -like $SpecialFolderName
  68.             Write-Verbose ('    Found {0} matching items.' -f $SFN_WorkingList.Count)
  69.  
  70.             foreach ($SWL_Item in $SFN_WorkingList)
  71.                 {
  72.                 Write-Verbose ('    Processing [ {0} ] ...' -f $SWL_Item)
  73.                 $SFPath = [environment]::GetFolderPath($SWL_Item)
  74.                 # fill in blank path with $NoPathFound value
  75.                 $SFPath = ($NoPathFound, $SFPath)[[bool]$SFPath]
  76.  
  77.                 $TempObject = [PSCustomObject]@{
  78.                     SpecialFolderName = $SWL_Item
  79.                     Path = $SFPath
  80.                     }
  81.  
  82.                 switch ($WhatToReturn)
  83.                     {
  84.                     'All'
  85.                         {
  86.                         $TempObject
  87.                         break
  88.                         }
  89.                     'NameOnly'
  90.                         {
  91.                         $TempObject.SpecialFolderName
  92.                         break
  93.                         }
  94.                     'PathOnly'
  95.                         {
  96.                         $TempObject.Path
  97.                         break
  98.                         }
  99.                     } # end >> switch ($WhatToReturn)
  100.                 } # end >> foreach ($SWL_Item in $SFN_WorkingList)
  101.             } # end >> process
  102.         #endregion >> process
  103.  
  104.         #region >> end
  105.         end
  106.             {
  107.             # nothing here for now
  108.             }
  109.         #endregion >> end
  110.  
  111.         } # end >> function Get-SpecialFolderInfo
  112.  
  113.  
  114.     #region >> testing parameters
  115.  
  116.     #Get-SpecialFolderInfo
  117.     #Get-SpecialFolderInfo -Verbose
  118.  
  119.     #Get-SpecialFolderInfo 'mymusic'
  120.     #Get-SpecialFolderInfo 'mymusic' -WhatToReturn PathOnly
  121.  
  122.     #Get-SpecialFolderInfo -SpecialFolderName 'common*' -Verbose
  123.     #Get-SpecialFolderInfo -SpecialFolderName 'common*'
  124.  
  125.     Get-SpecialFolderInfo -SpecialFolderName 'mydocuments' -WhatToReturn All -Verbose
  126.     #Get-SpecialFolderInfo -SpecialFolderName '*music*' -WhatToReturn NameOnly
  127.     #Get-SpecialFolderInfo -SpecialFolderName '*video*' -WhatToReturn PathOnly
  128.  
  129.     #endregion >> testing parameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement