+ Post New Thread
Results 1 to 7 of 7
Coding Thread, PowerShell Script Help in Coding and Web Development; I have a script that i'm looking at for copying a start menu locally and then removing the items that ...
  1. #1

    Join Date
    Feb 2007
    Location
    Wolverhampton
    Posts
    330
    Thank Post
    18
    Thanked 35 Times in 33 Posts
    Rep Power
    21

    PowerShell Script Help

    I have a script that i'm looking at for copying a start menu locally and then removing the items that dont exist. The script works if you specify each item at the bottom, but i think this can be simplified by using the ForEach command so that i don't have to specify each shortcut. Can anybody help

    Code:
    # Creates 'StartMenu' folder on C: Drive if it doesn't already exist.
    
    if(!(Test-Path "C:\StartMenu\Staff")){
        New-Item "C:\StartMenu\Staff" -ItemType directory
    }
    
    # Removes contents of 'StartMenu' folder and then replaces with fresh copies off of the Server.
    
    Remove-Item C:\StartMenu\Staff -recurse -force
    Copy-Item \\servername\_StartMenu\x64\Staff  C:\StartMenu\Staff -recurse -force
    
    # At this point the StartMenu folder on the C Drive is full of all shortcuts from corresponding Remote StartMenu.
    
    # Creates a varialbe called $SM that will hold C:\StartMenu path.
    
    $SM = "C:\StartMenu\Staff"
    
    # Function which will test path and then delete folder/lnk as instructed.  Syntax should be;
    # StartMenu %path to test% %folder to delete%.
    # e.g. StartMenu "C:\Program Files\WinRAR" "WinRAR"
    
    Function StartMenu {
    param ($TestPath,$Directory)
    
    if(!(Test-Path $TestPath)){
        Remove-Item $SM\$Directory -recurse -force
        }
    }
    
    
    # Function that will delete any empty folders that cannot be deleted via StartMenu due to containing
    # different programs.
    
    Function StartMenuDelete {
    param ($folderpath)
    
    
    if(!(Get-ChildItem $folderpath -recurse)){
    	Remove-Item "$folderpath"
        }
     }
    
    
    #StartMenu "C:\Program Files (x86)\TechSoft Design Tools\2D Design V2" "2D Design"

  2. #2

    FN-GM's Avatar
    Join Date
    Jun 2007
    Location
    UK
    Posts
    15,852
    Thank Post
    877
    Thanked 1,681 Times in 1,460 Posts
    Blog Entries
    12
    Rep Power
    445
    I have a VBS script that does the exact same if your interested?

  3. #3
    ascott2's Avatar
    Join Date
    Nov 2007
    Posts
    181
    Thank Post
    18
    Thanked 37 Times in 29 Posts
    Rep Power
    20
    You could have a csv file holding all your software information in, like Path, Name C:\Program Files (x86)\TechSoft Design Tools\2D Design V2,2D Design etc.

    Then use Import-CSV C:\staffsoftware.csv | Foreach{ Startmenu $_.Path $_.Name}

    Not really sure this is much of a help as instead of having all the software in the scripts it just moved to a csv. I suppose you could have software everyone uses in one csv file all the staff stuff in one, all student software in another and specialist software in their own then depending on which groups you want you could import those individual csvs.

  4. #4

    Join Date
    Jul 2012
    Location
    Boerne Texas USA
    Posts
    45
    Thank Post
    0
    Thanked 9 Times in 9 Posts
    Rep Power
    6
    # Creates a varialbe called $SM that will hold C:\StartMenu path.
    $SM = "C:\StartMenu\Staff"

    # Creates 'StartMenu' folder on C: Drive if it doesn't already exist.
    if(!(Test-Path $SM)){
    New-Item $SM -ItemType directory
    }


    # Removes contents of 'StartMenu' folder and then replaces with fresh copies off of the Server.
    Remove-Item $SM -recurse -force
    Copy-Item \\servername\_StartMenu\x64\Staff $SM -recurse -force

    # At this point the StartMenu folder on the C Drive is full of all shortcuts from corresponding Remote StartMenu.

    $lnks = ls $SM -Recurse -Filter "*.lnk"
    foreach ($shortcut in $lnks) {
    $WshShell = New-Object -ComObject WScript.Shell
    $link = $WshShell.CreateShortcut($shortcut.fullname)
    if(!(Test-Path $link.TargetPath)) {
    Remove-item $shortcut.fullname -force
    }
    }

  5. Thanks to jklight from:

    Arthur (20th July 2012)

  6. #5


    Join Date
    Feb 2007
    Location
    51.405546, -0.510212
    Posts
    8,778
    Thank Post
    223
    Thanked 2,633 Times in 1,940 Posts
    Rep Power
    780
    @jklight. I hope you don't mind, but I made a small addition to your script.

    Code:
    $ErrorActionPreference = "SilentlyContinue"
    
    # Create a variable called $SM that will hold C:\StartMenu path.
    $SM = "C:\StartMenu\Staff"
    
    # Creates 'StartMenu' folder on C: Drive if it doesn't already exist.
    if(!(Test-Path $SM)) {
        New-Item $SM -ItemType Directory
    }
    
    # Removes contents of 'StartMenu' folder and then replaces with fresh copies off of the Server.
    Remove-Item $SM -Recurse -Force
    Copy-Item \\ServerName\_StartMenu\x64\Staff $SM -Recurse -Force 
    
    # At this point the StartMenu folder on the C Drive is full of all shortcuts from corresponding remote StartMenu.
    $lnks = ls $SM -Recurse -Filter "*.lnk"
    ForEach ($Shortcut in $Lnks) {
        $WshShell = New-Object -ComObject WScript.Shell
        $Link = $WshShell.CreateShortcut($Shortcut.Fullname)
    
        if(!(Test-Path $Link.TargetPath)) {
            Remove-item -LiteralPath $Shortcut.Fullname -Force
        } 
    }
    Without 'LiteralPath', PowerShell won't be able to delete invalid shortcuts that have [square brackets] in their filenames.

  7. #6

    Join Date
    Jul 2012
    Location
    Boerne Texas USA
    Posts
    45
    Thank Post
    0
    Thanked 9 Times in 9 Posts
    Rep Power
    6
    Good catch!

  8. #7


    Join Date
    Oct 2006
    Posts
    3,411
    Thank Post
    184
    Thanked 356 Times in 285 Posts
    Rep Power
    148
    Ignore. Misread the question
    Last edited by j17sparky; 3rd May 2013 at 02:19 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Logon Script Help
    By djones in forum Scripts
    Replies: 17
    Last Post: 13th March 2008, 06:00 PM
  2. Proxy Authentication Script - HELP!!!!!
    By chrisa in forum Scripts
    Replies: 9
    Last Post: 7th March 2008, 04:34 PM
  3. Replies: 0
    Last Post: 24th January 2008, 02:58 PM
  4. AutoIt Script Help - Get Last Changed File
    By russdev in forum Scripts
    Replies: 8
    Last Post: 8th August 2007, 05:34 AM
  5. One PC refuses to run script! Help!!!
    By woody in forum Scripts
    Replies: 6
    Last Post: 5th October 2005, 11:37 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •