Powershell Command/Script to search and delete folders and sub folders - windows

good day, i was searching all over the internet for a powershell command/script that can search and delete any folder named (Games*) (game*) on remote machines
on CMD i used to command "del /s \computername\c$\Game*" and it works perfectly with me however it delete files only, need a powershell script that can delete files and folders.

Make use of pipeline option in powershell .Below code will do the work.
get-childitem -path 'FilePath' -Recurse -Filter "Game*" | remove-item -Force
Hope it HElps.

Remove-Item \\compname1\c$\Game* -force -recurse

Related

Using the matched wildcards in cmd

Can we access the matched character in wildcards?
I have some files in my current directory name as Labx.pdsprj and I want to copy them to a subfolder in the same directory based on their file names such that "Lab3.pdsprj" goes to the subfolder ".//subfolder_name//lab3" as ".//subfolder_name//lab3//Lab3.pdsprj"
I was trying something like
copy Lab?.pdsprj .//subfolder_name//lab(something here to access the matched number)
Its not necessary to use the above command only, any help is appreciated.
This may seem excessive if your requirement is not to have a case difference in the filename "Lab*" and the subdirectory "lab" as indicated by #dave_thompson_085. Although, that solution presumes that the "lab*" subdirectory already exists. I could be changed to do this.
FOR %%A IN (Lab?.pdsprj) DO (
MKDIR "%%~nA"
COPY %%A .\subfolder\%%~nA\%%~nxA
)
If there must be a case difference, this could be used. This will run on a windows command-line batch-file running under cmd. If you are on a supported Windows system, PowerShell was installed with it on the system.
powershell -NoLogo -NoProfile -File "C:\src\t\Do-CopyPdsprjFiles.ps1"
=== C:\src\t\Do-CopyPdsprjFiles.ps1
Get-ChildItem -File -Filter 'Lab*.pdsprj' |
ForEach-Object {
if ($_.Name -match 'Lab(\d+).pdsprj') {
$LabNumber = $Matches[1]
$LabDir = Join-Path -Path '.' -ChildPath "lab$($LabNumber)"
if (-not (Test-Path -Path $LabDir)) { mkdir $Labdir | Out-Null }
Copy-Item -Path $_.FullName -Destination $LabDir
}
}

Need script to move folders based on certain file types in the folder

I have a scenario where I need to search folders and sub folders for certain file types. if those types are found then the whole root folder with all files and sub folders needs to be moved.
Source c:\testsource
example: search for *.exe files
the following file is found
c:\testsource\folder1\subfolder1\testfile.exe
then folder1 with all files and subfolders needs to be moved to c:\testdestination
I first tried this with a batch file and was able to move all files in subfolder1 but not any of the other files or directory structure. I then started working on it in powershell but had similar results. I think what I need is to search, and if found capture the folder path, then move the folder, but not sure how to do that.
Batch file I created:
for /r "C:\TestSource" %i in (*.exe)do move "%~dpi\*" "C:\TestDestination\"
Powershell Script
$Source = "C:\testsource"
$Dest = "C:\testdestination"
Get-ChildItem -Recurse -Path $Source | Where {$_.fullname -Match '.*\.exe'} | Move-Item -Destination $Dest
Any help here would be much appreciated
You will need a loop to check for each direct subfolder of the source folder if it has the desired file in one of its subfolders. ... something like this:
$Source = 'C:\testsource'
$Dest = 'C:\testdestination'
Get-ChildItem -Path $Source -Directory |
ForEach-Object{
If (Get-ChildItem -Path $_.FullName -Recurse -File -Filter '*.exe'){
Move-Item -Path $_.FullName -Destination $Dest -Recurse
}
}

Copy all .jpg from subfolders to a single folder by cmd

I have a folder that has 148 folders in it, and in each of these folders they have a .jpg file. I need to get all these .jpg and put in a single folder
each folder is at least 1 .jpg
I can do this on Linux as well
`#!/bin/bash
for file in `find source -name * .jpg`;
of the mv "$ file" Destination;
done;
but I can not play this in Windows. Only the find command that I can reproduce the same result: dir /S /B *.jpg
for /r %f in (*.jpg) do move %f %destination%
Note: This is the interactive version. In a command script, you need to protect the %f's from too-early variable substitution.
for /r %%f in (*.jpg) do move %%f %destination%
Another way to do it using PowerShell. When you see that the files are being moved as you expect, remove the WhatIf from the Move-Item cmdlet.
Get-ChildItem -Recurse -File -Filter '*.jpg' |
ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }
You can run this in a cmd.exe shell or .bat script.
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem -Recurse -File -Filter '*.jpg' |" ^
"ForEach-Object { Move-Item -Path $_.FullName -Destination 'C:\the\other\dir' -WhatIf }"
The easiest way to achieve this is in Windows is to open to root folder with the Windows Explorer at which you want to start searching all the elements. Then go to the small input field for searching and enter *.jpg the result will show all the jpg in the root folder and all subfolders. Then you can simply copy and paste the files to your destination folder.

Windows Right-Click copy vs Powershell Copy-Item

I want to copy a directory containing log files, but my script is timing out when using powershell to perform the copy. If I simply right-click the log directory and copy it, it works fine.
Copy-Item -Path "$logpath" -Destination "$archivepath" -Recurse
The log directory is actively being written to which is what is causing the time-out. How can I copy this directory with powershell? I can copy it using Right-click but I can't with powershell.
Using Powershell v5
$logpath="C:\Path\To\LogDir"
$backuppath="C:\Path\To\BackupDir"
Set-Clipboard -Path "$logpath"
Get-Clipboard -Format FileDropList | Copy-Item -Destination "$backuppath" -Recurse
Using Robocopy
Robocopy.exe "$logpath" "$backuppath" /E | Out-Null

I want to remove all contents of %temp% directory wihout any confirmation

I just want to delete %temp% folder of that particular user that is currently logged in without any confirmation using a PowerShell script.
This is what I have tried so far:
$tempRM = #("C:\Users*\Appdata\Local\Temp*")
Remove-Item -Path $tempRM -Recurse -Force
OS will be mostly server 2003,2008,2012 and above.
Use the $env:TEMP environment variable to determine the temp path and delete it using the Remove-Item cmdlet with the -ErrorAction parameter set to SilentlyContinue to supress any potential errors:
Remove-Item -Path $env:TEMP -Recurse -Force -ErrorAction SilentlyContinue

Resources