Delete files without an extension in PowerShell - windows

I want to recursive delete all files which have no extension.
I already have the following command to delete files which have a specific extension:
Remove-Item <PATH> -include *.tmp -force -recurse

Get-ChildItem "yourpath" -file -recurse | where {-not $_.extension} | Remove-Item -WhatIf
Note: remove the -whatif to apply the action. -file will only list files and not folders
using the include in the get-childitem will restrict the scope to just that extension.

Here's what worked for me:
Remove-Item C:\Test\*

Related

How to move set of files having multiple extension to another directory using PowerShell?

I need to move set of specific files having different extension to another folder.
I have following filtered files in the directory.
file1.txt
file2.xml
file3.dll
I have kept the above files in the variable $files and I need to move each of files to another folder.
Below is the code I tried.
foreach ($fileType in $files) {
Get-ChildItem -Path C:\Files -Filter "$fileType*." -Recurse |
Move-Item -Destination C:\Dest
}
I am getting following error
Get-ChildItem : Illegal characters in path.
At line:1 char:38
+ ... lude_files){Get-ChildItem -Path C:\Files
Appreciate if anyone can help on this?
An easy way to do this
ls C:\files | Foreach {
Move-Item -Path C:\files\$filetype -Destination C:\dest
}
If all the files you are after share a common 'starts-with' name like file as in your example, the below should do what you want. It uses the -Include parameter where you can add an array of (in this case) extensions to look for.
Get-ChildItem -Path 'C:\Files' -Filter 'file*' -Include '*.txt','*.xml','*.dll' -Recurse |
Move-Item -Destination 'C:\Dest'
Note: the -Include parameter only works when also used together with the -Recurse switch, OR by appending \* after the path (like in C:\Files\*)

Converting find and copy in bash to some in Windows PowerShell

I am trying to convert code from bash to PowerShell like this:
In bash:
find ./searchfolder -type f -name "something" | xargs cp -t ./destinationfolder
I mean "find" command finds
./searchfolder/something
./searchfolder/0.15/something
./searchfolder/0.25/something
and "copy" command copies the files in the new directory (with preserving folder structure)
./destinationfolder/something
./destinationfolder/0.15/something
./destinationfolder/0.25/something
How can i do that? Thanks in advance.
How about using the Filter parameter on the Copy-Item command? This might get you close.
cp ".\searchfolder" -Recurse -Filter "something" -Destination ".\destinationfolder"
cp is an Alias of Copy-Item BTW.
This is assuming you are looking for text files in C:\temp and moving them to C:
Get-Childitem –Path C:\Temp -Recurse -Include *.txt* | ForEach-Object { mv $_.fullName c:\ }
Regards!
I am assuming this will be the folder structure. Only one Subdirectory inside Sourcefolder.
SourceFolder
--f1
-test.txt
--f2
-test.txt
--f3
-other.txt
and copy folders which has file named test
Destination
--f1
-test.txt
--f2
-test.txt
Script
cd D:\Vincent\PSTesting
$Path = 'Searchfolder'
Get-ChildItem $Path -Recurse -Filter *Test* -File | foreach {
$SourceFolder = $_.Directory -replace "^.*$Path"
Copy-Item -Path $Path\$SourceFolder -Destination "D:\Vincent\PSTesting\Destinationfolder\" -Verbose -Recurse
}

PowerShell script to remove file from hidden folders

I am currently trying to run a script I wrote. It works great, but I need it to also search and remove from the hidden folders as well. It does not seem to have any effect on hidden folders... Here is my script.
Get-ChildItem C:\ -Include saplogon.ini -Recurse | foreach ($_) {Remove-Item $_.fullname}
$src_dir = "\\xxxxxxxxxx\xxxxxxxxxxxx\saplogon\saplogon.ini"
$dst_dir = "C:\Windows"
$file = Get-ChildItem $src_dir
Copy-Item $file -Destination $dst_dir
[System.Environment]::SetEnvironmentVariable('SAP_LOGON_INI', 'C:\Windows\saplogon.ini', 'Machine')
You are missing the the -Force parameter.
The code below is using alias so it won't require horizontal scrolling. Know that gci is Get-ChildItem.
Note that you will only be able to get access if you have permission.
gci c:\ -Include saplogon.ini -Recurse -Force | foreach ($_) {remove-item $_.fullname}
At this point, you probably already took care of the non-hidden files. If you want to run the script again, but only for hidden files (and not non-hidden files), you can do that with the -Hidden flag.
Again, you will only be able to get access if you have the permission.
gci c:\ -Include saplogon.ini -Recurse -Hidden | foreach ($_) {remove-item $_.fullname}

Recursively copy a set of files from one directory to another in PowerShell

I'm trying to copy all *.csproj.user files recursively from C:\Code\Trunk to C:\Code\F2.
For example:
C:\Code\Trunk\SomeProject\Blah\Blah.csproj.user
Would get copied to:
C:\Code\F2\SomeProject\Blah\Blah.csproj.user
My current attempt is:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination
C:\Code\F2 -Recurse -WhatIf
However I get:
What if: Performing operation "Copy Directory" on Target "Item:
C:\Code\Trunk Destination: C:\Code\F2\Trunk".
First, it wants to put them all in a new folder called F2\Trunk which is wrong. Second, it doesn't list any of the files. There should be about 10 files to be copied over.
What's the correct syntax for the command? Thanks!
Update:
Okay, it seems to have something to do with the fact that C:\Code\F2 already exists. If I try copying the files over to a destination that does not exist, it works.
I want to overwrite any existing .csproj.user files in the destination.
You guys are making this hideously complicated, when it's really simple:
Copy-Item C:\Code\Trunk -Filter *.csproj.user -Destination C:\Code\F2 -Recurse
Will copy the Directory, creating a "Trunk" directory in F2. If you want to avoid creating the top-level Trunk folder, you have to stop telling PowerShell to copy it:
Get-ChildItem C:\Code\Trunk | Copy-Item -Destination C:\Code\F2 -Recurse -filter *.csproj.user
While the most voted answer is perfectly valid for single file types, if you need to copy multiple file types there is a more useful functionality called robocopy exactly for this purpose with simpler usage
robocopy C:\Code\Trunk C:\Code\F2 *.cs *.xaml *.csproj *.appxmanifest /s
Seen this before, and I don't know why PowerShell can't seem to get it right (IMHO). What I would do is more cumbersome but it works.
$Source = 'C:\Code\Trunk'
$Files = '*.csproj.user'
$Dest = 'C:\Code\F2'
Get-ChildItem $Source -Filter $Files -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Copy-Item $_.FullName -Destination $Path -Force
}
I tried Jaykul answer and it did not work for me. I had to change it as below to get it to work. I also created the C:\Code\F2 folder before it worked.
Get-ChildItem C:\Code\Trunk -Recurse -filter *.csproj.user | Copy -Destination C:\Code\F2
Answer 1 looked good, and I changed to Move-Item for my purposes. However I found that in each folder it recursively went through, it only moved the first file. Below is my complete script which also includes some conversion of doc files to pdf's:
$Source = 'C:\Users\sgrody\Desktop\NSPM-Vol1'
$MoveFiles = '*.PDF'
$Dest = 'C:\Users\sgrody\Desktop\MedPassPDF'
$Folders = Get-ChildItem $Source -Directory -Recurse
ForEach ($Folder in $Folders)
{
$wdFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$folderpath = "$($Folder.FullName)\*"
$fileTypes = "*.docx","*doc"
Get-ChildItem -path $folderpath -include $fileTypes |
foreach-object
{
$path = ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
"Converting $path to pdf ..."
$doc = $word.documents.open($_.fullname)
$doc.saveas([ref] $path, [ref]$wdFormatPDF)
$doc.close()
}
$word.Quit()
}
Get-ChildItem $Source -Filter $MoveFiles -Recurse | ForEach{
$Path = ($_.DirectoryName + "\") -Replace [Regex]::Escape($Source), $Dest
If(!(Test-Path $Path)){New-Item -ItemType Directory -Path $Path -Force | Out-Null
Move-Item $_.FullName -Destination $Path -Force
}
}
Recently I had to replace a file present in several sub folders, I did it as below.
foreach($file in (Get-ChildItem File_you_want_to_be_copied.txt -Recurse)) {$target=$file.directoryname; Copy-Item -path C:\Source_Path\File_you_want_to_be_copied.txt -Destination $target}

how can i recursively rename folders in windows matching string and ignoring case

I have subfolders which have names:
original_Optimize
Original_optimize
original_optimize
Original_Optimize
I would like to rename all of these to:
Original_Optimize
Is there an easy way of doing this in windows (perhaps using powershell or something in command prompt ) ?
You can do that in two Rename-Item calls. The first would add a prefix to each name to avoid the 'Source and destination path must be different.' error. The second run will remove the prefix.
Get-ChildItem -Filter original_optimize -Recurse |
Rename-Item -NewName __foo__Original_Optimize -PassThru |
Rename-Item -NewName {$_.Name -replace '^__foo__'} -PassThru
Try this.
Get-ChildItem C:\path-to-directory -Recurse -Filter *foo* | Rename-Item -NewName { $_.name -replace 'foo', 'bar'} -verbose

Resources