Script to give a list of all MP3 files in a user folder and record a list to a central depository before deleting

Hello all,

I need help with a little project that I am working on. I need to make a script (can be Powershell, batch, or vbscript) that scans the userfolder of a user's computer for all music and video files. It must report the location of files to a central repository and then delete them.

I have a few batches that do this, but not all in one script. We use Kaseya for our computer management, so I would like to use it to deploy and execute the script.

Please note, the computer name is the same as the username of the user that would be logged in to the computer.

Scripts I have now,
*this one does not work right. it sometimes will give me the directory of the user's network user folder rather than the user folder on the computer*

cd c:\users\%username%
dir /s *.avi >> \\servername\students\logs\%username%-avi.txt
dir /s *.mov >> \\servername\students\logs\%username%-movi.txt
dir /s *.MP3 >> \\servername\students\logs\%username%-mp3.txt
dir /s *.M4A >> \\servername\students\logs\%username%-m4a.txt

*This one works, but I cannot figure out how to add a results file and it scans the entire hard disk*
for /f "tokens=*" %%j in ('dir /B /s *.mp3') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.mp4') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.m4a') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.swf') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.avi') do del /Q "%%j"
for /f "tokens=*" %%j in ('dir /B /s *.mov') do del /Q "%%j"


I suppose I could try combining the two into a single batch, but there has to be something more elegant.

Thanks.
VillaMariaAsked:
Who is Participating?
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Try that

$user = dir env:username
$username = "c:\users\" + ($user).value
$Userfile = "\\studentshareserver\students\logs\" + ($user).value + ".txt"

get-childitem -path $username -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | Export-CSV $userfile

Krzysztof
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
I would suggest POwerShell script for that. You can do that using this syntax

Get-ChildItem -Path C:\ -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\output.csv

for logging

and then to remove them

Get-ChildItem C:\ -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Regards,
Krzysztof
0
 
VillaMariaAuthor Commented:
So would something like this work?

Get-ChildItem -Path C:\users\%username% -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\output.csv

And

Get-ChildItem -Path C:\users\%username% -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

My users can only save in their User folders so I do not want to delete any files used by software installed on their machines by doing this search at the root of the drive.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Actually for that you need to declare user's variable. Chect that please

$user = dir env:username
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Krzysztof
0
 
VillaMariaAuthor Commented:
OK so, this is what I've come up with.

$user = dir env:username
Get-ChildItem -Path C:\users\($user) -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\($user).csv
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item
0
 
VillaMariaAuthor Commented:
or should I split it into two scripts?
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
No, it's fine. It can be in one script. But in second script line is small error :) You need to also use ($user).value
So use these lines

$user = dir env:username
Get-ChildItem -Path C:\users\($user).value -Recurse -Include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Select name,Directory | Export \\UNCShare\($user).value".csv"
Get-ChildItem -Path C:\users\($user).value -recurse -include *.mp3,*.mp4,*.m4a,*.swf,*.avi,*.mov | Remove-Item

Krzysztof
0
 
VillaMariaAuthor Commented:
This is what I have had to do to get the first part of the script working. I have not gotten the export to work. Export is not a valid command. I found two other ways of exporting export-csv and out-file..I have tried both and neither seem to like trying to name the file as the username. I can get it to work if I name the file with a fixed name, but not a name that comes from a variable.

$user = dir env:username
$username = "c:\users\" + $user
$Userfile = "\\studentshareserver\students\logs\" + $user + ".txt"

get-childitem -path ($username).value -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | out-file ($userfile).value
1
 
VillaMariaAuthor Commented:
nope. Saves the file as System.collections.dictionaryEntry.txt
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
OK, I would check that tomorrow morning in my test lab and I post results here

Krzysztof
0
 
VillaMariaAuthor Commented:
Got it!

$user = dir env:username
$username = "c:\users\" + $user
$Userfile = "\\studentshareserver\students\logs\" + ($user).value + ".csv"

get-childitem -path ($username).value -recurse -include *.mp3,*.mp4,*.swf,*.avi,*.mov | Select name,Directory | ft directory, name |export-csv $userfile

Wierd that $username and $userfile are basically the same process for assigning the values, but $userfile has to be that way.
0
 
VillaMariaAuthor Commented:
Thanks so much. I never would have gotten there without your help.
0
 
Krzysztof PytkoSenior Active Directory EngineerCommented:
Great! Glad I could help :)

Krzysztof
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.