Powershell script against list of computers

Posted on 2014-12-10
Last Modified: 2014-12-11
I want to run a script against a list of computers in order to remove some shortcuts from the public desktops.  question is two fold- top set is win7 bottom is XP.  are both of those correct?  I could not determine the working "public" variable for XP.

second is how do write this to pull from a text file with a list of computer names?

Remove-Item -Path "$env:public\Desktop\Microsoft Access 2010.lnk"
Remove-Item -Path "$env:public\Desktop\Microsoft Excel 2010.lnk"
Remove-Item -Path "$env:public\Desktop\Microsoft Outlook 2010.lnk"
Remove-Item -Path "$env:public\Desktop\Microsoft Word 2010.lnk"
Remove-Item -Path "$env:public\Desktop\adobe reader XI.lnk"

Remove-Item -Path "c:\all users\Desktop\Microsoft Access 2010.lnk"
Remove-Item -Path "c:\all users\Desktop\Microsoft Excel 2010.lnk"
Remove-Item -Path "c:\all users\Desktop\Microsoft Outlook 2010.lnk"
Remove-Item -Path "c:\all users\Desktop\Microsoft Word 2010.lnk"
Remove-Item -Path "c:\all users\Desktop\adobe reader XI.lnk"
Question by:GreshAssoc
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
LVL 67

Accepted Solution

sirbounty earned 500 total points
ID: 40492766
According to it's %public% for everything after Vista.  XP didn't have a "public" per se, I believe it was %ALLUSERSPROFILE%
LVL 67

Assisted Solution

sirbounty earned 500 total points
ID: 40492784
Second part of your question takes on some assumptions, but seems like it should work for you:
*Code is 'non-destructive' when used 'as-is'...
$shortcuts = 'Microsoft Access 2010','Microsoft Excel 2010', 'Microsoft Outlook 2010','Microsoft Word 2010','adobe reader XI'

foreach ($pc in get-content C:\TestFolder\myComputers.txt) {
    $OS = Get-WmiObject -Computer $pc -Class Win32_OperatingSystem 
    #assumes you don't have anything other than XP and Vista/Win7...
    if ($OS.caption -like '*xp*'){
        $path = "\\$pc\c$\documents and settings\all users\desktop"
    } else {
      $path = "\\$pc\c$\users\public\desktop"
    foreach ($shortcut in $shortcuts) {
        #remove "WhatIf" parameter to enforce remove-item
        remove-item -Path "$path\$shortcut.lnk" -WhatIf

Open in new window


Author Comment

ID: 40492867
awesome, so that seems to work well. now what if I wanted to remove an all user start menu program too?  do I just add in those paths too?
else {
$path = "\\$pc\c$\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office"
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

LVL 67

Expert Comment

ID: 40492892
Everything beneath that path?  If it contains subfolders, you'll need to include the -recurse parameter

Author Comment

ID: 40492917
No subfolders there. Would only be the office programs lnk's again. Idea is to get them of the desktop and the programs menu
LVL 67

Expert Comment

ID: 40492945
In that case, I think you can just perform a remove-item and specify *.lnk for the files, using the -recurse parameter.  That will remove and and all shortcuts underneath that path...
LVL 67

Expert Comment

ID: 40494588
Glad I could help - thanks for the grade. :^)

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question