Update-StartMenu :Cleans up Start Menu after an install

//Update-StartMenu :Cleans up Start Menu after an install
Update-StartMenu :Cleans up Start Menu after an install 2017-07-06T17:06:30+00:00

The Toolkit Forums Toolkit Extensions Update-StartMenu :Cleans up Start Menu after an install

  • Author
    Posts
  • Mark
    Participant
    Post count: 6
    #2195 |

    There are a couple of things here that are specific to our environment, but I made this to keep the start menu clean of unneeded folders and links that the end user doesn’t need to see. Also this allows for a Staff Only (we’re in education and these are links the Students shouldn’t see) and TSA Only (only links for Techs) folder with appropriate rights set. I use the, very nice, NTFSSecurity module from https://github.com/raandree/NTFSSecurity to set permissions.

    Since I can’t figure out how to paste code in here, I put it here: https://gist.github.com/macxcool/15c81f3b23ead6810d7ddb2039f8fb78

    <code>
    #region Function Update-StartMenu
    Function Update-StartMenu {
    &amp;lt;#
    .SYNOPSIS
        Cleans up the Start Menu by moving shortcuts and deleting unwanted shortcuts and folders.
    .DESCRIPTION
        Moves shortcuts to the root or a folder of the Menu and deletes unwanted shortcuts and leftover folders.
    .PARAMETER Folder
        The name of the Start Menu subfolder to work in
        Set this to $strStaffOnly if the links should be hidden to students.
        Set this to $strTSAOnly if the links should only be visible to TSAs
    .PARAMETER GoodLinks
        An array of shortcut names to keep and move to the Menu root. &#039;All&#039; will keep all links... not useful in some contexts.
    .PARAMETER $KeepFolder
        A Switch parameter that retains the folder but keeps only the listed links. Can&#039;t be used with $OtherFolder or $MoveLinks
    .PARAMETER $OtherFolder
        A switch that creates a folder (if necessary) into which the links will be placed. Links from anywhere in the Start Menu 
        can be used here. The script will look for them recursively. Only filenames unique in the hierarchy will work.
        Can&#039;t be used with $KeepFolder or $MoveLinks
    .PARAMETER $MoveLinks
        The default mode. A Switch to tell the script to Move the links rather than keeping or creating a folder for them.
        This switch is assumed and so does not have to be specified.
    .EXAMPLE
        Update-StartMenu -Folder &#039;Greenshot&#039; -GoodLinks &#039;Greenshot&#039;,&#039;Readme&#039;
    .EXAMPLE
        Update-StartMenu -Folder &#039;Some App&#039; -GoodLinks &#039;All&#039;
    .EXAMPLE
        Update-StartMenu -Folder &#039;Some Other App&#039; -GoodLinks &#039;This App&#039;,&#039;App Website&#039; -KeepFolder
    .EXAMPLE
        Update-StartMenu -Folder &#039;_Staff-Only&#039; -Goodlinks &#039;One&#039;,&#039;Two&#039;,&#039;Three&#039;
    .NOTES
        Created by tv45057 for the TVDSB Deployment team
    #&amp;gt;
        [CmdletBinding(DefaultParametersetName=&#039;MoveLinks&#039;)]
        Param (
            [Parameter(Mandatory=$true,HelpMessage=&#039;Enter the name of a Start Menu subfolder.&#039;)]
            [ValidateNotNullorEmpty()]
            [string]$Folder,
            [Parameter(Mandatory=$true,HelpMessage=&#039;Enter a list of links to keep. &amp;quot;All&amp;quot; can be used to keep all links.&#039;)]
            [ValidateNotNullorEmpty()]
    		[string[]]$GoodLinks,
            [Parameter(ParameterSetName=&#039;KeepFolder&#039;)]
            [switch]$KeepFolder,
            [Parameter(ParameterSetName=&#039;OtherFolder&#039;)]
            [switch]$OtherFolder,
            [Parameter(ParameterSetName=&#039;MoveLinks&#039;)]
            [switch]$MoveLinks
        )
    
        # Set up a couple of useful variables
        $folderPath = &amp;quot;$envCommonStartMenuPrograms\$($Folder)&amp;quot;
        $staffFolder = $strStaffOnlyFolder
        $TSAFolder = $strTSAOnlyFolder
    
        # If we&#039;re using the Staff Only folder and it doesn&#039;t exist, create it and set the perms
        If ( !(Test-Path -Path $folderPath) -and ($Folder -eq $staffFolder) ) {
            New-Folder -Path $folderPath
    
            # Deny access to the Student group in the ACL. This should hide it
            Add-NTFSAccess -Path $folderPath -Account &#039;TVNET\Students&#039; -AccessRights FullControl -AccessType Deny
        }
    
        # If we&#039;re using the TSA Only folder and it doesn&#039;t exist, create it and set the perms
        If ( !(Test-Path -Path $folderPath) -and ($Folder -eq $TSAFolder) ) {
            New-Folder -Path $folderPath
    
            # Remove inheritance from the folder
            Disable-NTFSAccessInheritance -Path $folderPath
    
            # Remove access from Users and Everyone and give it to TSA Group
            Remove-NTFSAccess -Path $folderPath -Account &#039;BUILTIN\Users&#039; -AccessRights ReadAndExecute,Synchronize
            Remove-NTFSAccess -Path $folderPath -Account &#039;Everyone&#039; -AccessRights ReadAndExecute,Synchronize
            Add-NTFSAccess -Path $folderPath -Account &#039;TVNET\TSA  Group&#039; -AccessRights FullControl
        }
    
    	#### Using the KeepFolder Mode #### Keep certain files in an existing Folder and delete the rest.
    	If ($KeepFolder) {
            # Check to see if the folder exists. This doesn&#039;t make much sense if it doesn&#039;t
            If (Test-Path -Path $folderPath -PathType Container) {
                # See if &#039;All&#039; is being used and it&#039;s the only thing in GoodLinks
                If (($GoodLinks -contains &#039;All&#039;) -and ($GoodLinks.Count -eq 1)) {
                    Write-Log -Message &#039;Warning: Doing nothing. This combination of parameters leaves everything as-is!&#039; -Source ${cmdletName}
                }
                # If &#039;All&#039; is not being used we can go ahead and remove anything not in GoodLinks
                ElseIf ($GoodLinks -notcontains &#039;All&#039;) {
                    Write-Log -Message &amp;quot;Removing unwanted links from Start Menu folder [$($Folder)]&amp;quot; -Source $cmdletName
                    # Get a full listing of the files in Folder
                    Get-ChildItem -Path $folderPath -File -Recurse | ForEach-Object {
                        # If this file&#039;s name isn&#039;t in GoodLinks then remove it.
                        If ($GoodLinks -notcontains $_.BaseName) {
                            Remove-File -Path $_.FullName
                        }
                    }
                }
                Else {
                    Write-Log -Message &#039;Error: It does not make sense to use [All] with anything else.&#039; -Source ${cmdletName}
                }
            }
            Else {
                Write-Log -Message &amp;quot;Error: Folder [$($Folder)] does not exist.&amp;quot; -Source $cmdletName
            }
        }
        #### Using the OtherFolder Mode #### Move files into another folder from anywhere in the Start Menu hierarchy.
        ElseIf ($OtherFolder) {
            # Using &#039;All&#039; with OtherFolder doesn&#039;t really make sense. We aren&#039;t going to move all the start menu files
            If (($GoodLinks -contains &#039;All&#039;) -and ($GoodLinks.Count -eq 1)) {
                Write-Log -Message &#039;Error: [All] cannot be used with [OtherFolder]. You need to specify lnk names.&#039; -Source $cmdletName
            }
            ElseIf ($GoodLinks -notcontains &#039;All&#039;) { # &#039;All&#039; is not being used, so go ahead
                If ( ($Folder -ne $staffFolder) -and ($Folder -ne $TSAFolder) ) { New-Folder -Path $folderPath }
    
                # Now it&#039;s time to move all the goodlinks files into the folder
                ForEach ($link in $GoodLinks) {
                    # Look recursively through the Start Menu for this filename and move it. Only names unique in the Start hierarchy will work.
                    $fileObj = Get-ChildItem -Path &amp;quot;$envCommonStartMenuPrograms&amp;quot; -Include &amp;quot;$($link).*&amp;quot; -File -Recurse
                    Write-Log -Message &amp;quot;Moving [$($fileObj.Name)] into folder [$($Folder)]&amp;quot; -Source $cmdletName
                    Move-Item -Path $fileObj.FullName -Destination $folderPath
                    #Write-Host &amp;quot;[$($fileObj.FullName)] to [$($envCommonStartMenuPrograms)\$($Folder)] &amp;quot;
                }
            }
            Else {
                Write-Log -Message &#039;Error: It does not make sense to use [All] with anything else.&#039; -Source ${cmdletName}
            }
        }
        #### Using the MoveFiles Mode #### Move files into the root of the Start Menu.
        Else {
            If (Test-Path -Path $folderPath -PathType Container) {
                If (($GoodLinks -contains &#039;All&#039;) -and ($GoodLinks.Count -eq 1)) {
                    Write-Log -Message &#039;Moving all Links into main Start Menu folder.&#039; -Source ${cmdletName}
    		        Get-ChildItem -Path $folderPath -File -Recurse | ForEach-Object {
    			        Copy-File -Path $_.FullName -Destination $envCommonStartMenuPrograms
    		        }
                    Remove-Folder -Path $folderPath
    	        }
    	        ElseIf ($GoodLinks -notcontains &#039;All&#039;) {
    		        ForEach ($link in $GoodLinks) {
                        $filePath = (Get-ChildItem &amp;quot;$folderPath\$($link).*&amp;quot; -File -Recurse).FullName
    			        Copy-File -Path $filePath -Destination $envCommonStartMenuPrograms
    		        }
                    Remove-Folder -Path $folderPath
    	        }
                Else {
                    Write-Log -Message &#039;Error: It does not make sense to use [All] with anything else.&#039; -Source ${cmdletName}
                }
            }
            Else {
                Write-Log -Message &amp;quot;Error: Folder [$($Folder)] does not exist. Cannot complete moving of links.&amp;quot;
            }
        }
    
    }
    #endregion
    </code>
    • This topic was modified 4 months, 2 weeks ago by  Mark. Reason: paste went horribly wrong

You must be logged in to reply to this topic.