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 ...
-
12th July 2012, 12:36 PM #1 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"
-
-
IDG Tech News
-
13th July 2012, 12:31 AM #2 I have a VBS script that does the exact same if your interested?
-
-
16th July 2012, 05:14 PM #3 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.
-
-
16th July 2012, 06:22 PM #4
- Rep Power
- 3
# 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
}
}
-
-
2nd May 2013, 10:48 PM #5 @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.
-
-
3rd May 2013, 01:35 PM #6
- Rep Power
- 3
-
-
3rd May 2013, 02:15 PM #7 Ignore. Misread the question
Last edited by j17sparky; 3rd May 2013 at 02:19 PM.
-
SHARE: 
Similar Threads
-
By djones in forum Scripts
Replies: 17
Last Post: 13th March 2008, 06:00 PM
-
By chrisa in forum Scripts
Replies: 9
Last Post: 7th March 2008, 04:34 PM
-
By fawkers in forum Scripts
Replies: 0
Last Post: 24th January 2008, 02:58 PM
-
By russdev in forum Scripts
Replies: 8
Last Post: 8th August 2007, 05:34 AM
-
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
-
Forum Rules