Jump to content

Recommended Posts

Posted

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

 

# 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"

Posted

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.

Posted

# 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

}

}

  • Thanks 1
  • 9 months later...
Posted

@jklight. I hope you don't mind, but I made a small addition to your script. :)

 

$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 [color="#FF0000"]-LiteralPath[/color] $Shortcut.Fullname -Force
   } 
}

 

Without 'LiteralPath', PowerShell won't be able to delete invalid shortcuts that have [square brackets] in their filenames.

Guest Guest
Posted (edited)
Ignore. Misread the question Edited by Guest

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...