Alex
asked on
powershell, again.... I should take up a new career
$Path = "$env:USERPROFILE\appdata\roaming\"
$Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
Get-childitem $Path -include $Files -recurse | foreach ($_) {remove-item $_.fullname}
Ok,
So this is meant to delete all files associated with the above file types, it doesn't. It also doesn't pull the array correctly, it only pulls *.PDF
I'm getting rather frustrated. Could someone help again please :(
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've even tried wrapping the command in { } and that makes no difference...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Nope they are still there and it's not doing anything with them, that was the first thing I checked.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Which version of Powershell are you using?
$PSVersionTable
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm on powershell version 5
ASKER
That has only removed the PDF files, would it be better to use a -or function between the file extensions?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
adding a * to your path makes your script work. remove the measure command block.. just adding the * makes it the fastest
measure-command {
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
Get-childitem $Path -include $Files -recurse
}
ASKER
Tony,
Yes, running the $files with the array and then $files on it's own shows the file types
David, your script didn't delete the files.
This is beyond stupid, I have no idea why this isn't working
Yes, running the $files with the array and then $files on it's own shows the file types
David, your script didn't delete the files.
This is beyond stupid, I have no idea why this isn't working
Is array displayed horizontaly or verticaly?
David's script does not contain remove-item, it will delete files, when you will add remove-item. ;)
David's script does not contain remove-item, it will delete files, when you will add remove-item. ;)
ASKER
ASKER
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
which means
Should work, it doesn't.
I'm leaving this till tomorrow now anyway, it's getting me angry now
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files="*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
Get-childitem $Path -include $Files -recurse | Remove-Item
Should work, it doesn't.
I'm leaving this till tomorrow now anyway, it's getting me angry now
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok right i'm making progress
That removes all the JPG files in the folder
It's definitely the array!
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files="*.JPG"
$files | ForEach-Object { Get-ChildItem -Path $path -Filter $_ -Recurse | Remove-Item}
That removes all the JPG files in the folder
It's definitely the array!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
qlemo
I've just tried
No luck
I've just tried
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= "*.pdf,*.doc,*.dot,*.docx,*.docm,*.dotx,*.dotm,*.docb,*.xls,*.xlt,*.xlm,*.xltx,*.xlsm,*.xlsb,*.xla,*.pptx,*.pptm,*.potx,*.potm,*.ppsx,*.ppsm"
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_ -Recurse | Remove-Item}
No luck
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Doing them individually works fine.
Doing more than 1 fails and doesn't throw back an error
Works great
Doing more than 1 fails and doesn't throw back an error
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= '*.xlsx'
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_ -Recurse | Remove-Item}
Works great
ASKER
RIGHT!
WE GOT IT GUYS, I LOVE YOU ALL SOOOO MUCH!
It was my variable missing something
It was the "*.xls","*.DOC" that I needed, it works so handing out points!
WE GOT IT GUYS, I LOVE YOU ALL SOOOO MUCH!
It was my variable missing something
$Path = "$env:USERPROFILE\appdata\roaming\*"
$Files= "*.pdf","*.doc","*.dot","*.docx","*.docm","*.dotx","*.dotm","*.docb","*.xlsx","*.xls","*.xlt","*.xlm","*.xltx","*.xlsm","*.xlsb","*.xla","*.pptx","*.pptm","*.potx","*.potm","*.ppsx","*.ppsm"
$files | ForEach-Object { Get-ChildItem -Path $path -Include $_ -Recurse | Remove-Item}
It was the "*.xls","*.DOC" that I needed, it works so handing out points!
But as suggested, you do not need the loop here. The single Get-ChildItem ... -Include $Files works best.
ASKER
It's running as admin
it's my profile
I'm getting a little hacked off tbh, it shouldn't be this difficult