Solved

PowerShell

Posted on 2014-11-17
6
158 Views
1 Endorsement
Last Modified: 2014-12-15
Good morning,

I have about 90 files in a temp directory on about 1000 machines named macro.x02 - macro.x99, possible you can give me a example of a powershell script that will delete all these files if found?    

Thanks
1
Comment
Question by:SDJ_1
  • 3
  • 3
6 Comments
 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 500 total points
ID: 40447318
Here you go, just change the MyTempdirectory to the folder the temp files reside. Here are 2 options, by list or by OU. If you have them sorted another way, let me know.

I kept "-whatif" on there so you can see what files it chooses. Once you are sure it is working how you want, remove "-whatif"

By a list of computers
$computers = Get-Content C:\ComputerList.txt | foreach {
Get-ChildItem "\$_\c$\MyTempDirectory" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}

Open in new window


By OU
$computers = Get-ADComputer -| foreach {
Get-ChildItem "\$_\c$\MyTempDirectory" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}

Open in new window

0
 

Author Comment

by:SDJ_1
ID: 40447671
Thanks for responding, I tried both versions and its failing.    I'm using Powershell version 2.    The script will be copied to all the affected machines and will be executed from a temp directory.
0
 

Author Comment

by:SDJ_1
ID: 40447680
I used the second option:

$computers = Get-ADComputer -| foreach {
Get-ChildItem "\$_\d$\Prgram Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
}


Getting the following error:

Get-ChildItem : Cannot find path '\\d$\Program files\POSWIN\Parm' because it does not exist.
At C:\Documents and Settings\Cambridge\Desktop\MacroDelete2.ps1:2 char:14
+ Get-ChildItem <<<<  "\$_\d$\Program files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}
    + CategoryInfo          : ObjectNotFound: (\\d$\Program files\POSWIN\Parm:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Assisted Solution

by:Joshua Grantom
Joshua Grantom earned 500 total points
ID: 40447684
If the script is being copied and run locally then you only need this

This just shows what will happen.
Get-ChildItem "D:\Program Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname -whatif}

Open in new window


This will actually delete them.
Get-ChildItem "D:\Program Files\POSWIN\Parm" -include "macro.x*" -recurse | foreach ($_) {remove-item $_.fullname}

Open in new window

0
 

Author Comment

by:SDJ_1
ID: 40447712
Ok that was perfect only thing I only need it to delete macro.x02 - .x99 only above option is awesome but its deleting everything.
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 500 total points
ID: 40447849
Here you go. Remove "-Whatif" to have it actually remove the files.

$files = 2..99 | % { "macro.x{0:00}" -f $_ }
foreach($file in $files) {
Get-ChildItem "D:\Program Files\POSWIN\Parm" -Include $ext -recurse | foreach {Remove-Item $_.fullname -WhatIf}
}

Open in new window

1

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 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