Powershell command to copy only text files - powershell

I am trying to write a Powershell command to identify and copy only text (.txt) files from within a directory and sub-directories of that directory. Does anyone know if there is a command that can perform this task.

If you're not worried about maintaining the directory structure you could use
Get-ChildItem *.txt -recurse | Copy-Item -destination c:\qwerty
If you would like to maintain the directory structure you could use
Copy-Item -Recurse -Filter *.txt -path c:\temp -destination c:\asdf
NB.
The PowerShell get-help command is very useful and it does except wildcards.
ie
get-help *copy* gives you a list of commands that might be useful to you.
get-help Copy-Item -full gives you all the parameters plus examples of usage.

Related

Writing a PowerShell script to copy directory and its contents to all users documents folder

First, this is my first REAL question on stack-overflow so here we go:
I am trying to use Get-ChildItem and Copy-Item to copy a directory and its contents to every user-profile that may be on a PC without knowing how many users or their usernames. However, I have run into an illegal character issue whenever I run the script or other variations of what I think are the same way to achieve the result. I think it is because of the wildcard but I do not know how I should circumvent that.
For Example:
Testing the Wildcard:
Get-ChildItem 'C:Users\*\'
I can see all the users and Power-shell Documentation from Microsoft states using the * should allow it so include all items in that folder. But whenever I extend the path to lets say:
Copy-Item 'D:\Custom Office Templates' -Recurse -Destination 'C:\Users\*\Documents\' -Force
I get an "Illegal Characters in path error."
If I replace that * with an actual username to correct the path I get what I want for a single user.
The same thing happens when trying to use.
ForEach-Object {Copy-Item -Path 'D:\Custom Office Templates' -Destination 'C:\users\*\Documents\Custom Office Templates' -Force -Recurse}
Thanks you for any assistance given :).
-Destination is read as a literal path, does not understand about wildcards. What you can do is combine what you already have with Get-ChildItem using a wildcard on -Path with a delay-bind script block on -Destination:
$source = 'D:\Custom Office Templates'
Get-ChildItem 'C:Users\*\' -Directory |
Copy-Item -LiteralPath $source -Recurse -Destination {
Join-Path $_.FullName 'Documents\Custom Office Templates'
}

Delete multiple files using powershell and a .txt file

I have a .txt with the names of over 1000 files I want to delete. My .txt file does not have file paths. The files I want to delete are spread throughout multiple folders but they are all in the same drive. Is there any way to use powershell or command prompt to search for all files within my drive with the same name as what is listed in my .txt file and delete them?
Assuming you're PowerShell prompt is currently set at the root location from which you want to start your search and the file is in the same directory:
gc .\MyListOfFilesIWantToDelete.txt | %{gci $_ -Recurse | Remove-Item -WhatIf}
Note, you'll have to remove the -whatif
Or, let's say your file is somewhere else where you have PowerShell opened (eg: ~/Documents), and you want to scan your D: drive. This should work:
gc .\MyListOfFilesIWantToDelete.txt | %{gci D:\ -Filter $_ -Recurse -ErrorAction SilentlyContinue | Remove-Item -WhatIf}
Note I put SilentlyContinue. This is because you'll see a lot of red if you don't have access to folders in your search path.
Alternatively, you can load up a variable with your list of files..
$thesefiles = gc .\mylistoffilesiwanttodelete.txt
.. and use the Remove-Item cmdlet directly..
Remove-Item -Path D:\Folder -Include $thesefiles -Recurse -WhatIf
or in one swoop without loading a variable:
Remove-Item -Path D:\Folder -Include $(gc .\mylistoffilesiwanttodelete.txt) -Recurse -WhatIf
Again, I'm using -WhatIf for testing. Also, I've noticed different behaviors in the past with get-childitem on different versions of PowerShell. I tested these with 5.1
Change directory from following powershell command
Following command will allow you to delete .txt files in specific directory
Get-ChildItem C:\*.txt -file -r | remove-item

How can I copy all files in a directory matching certain extensions with PowerShell?

I've done something like this before with batch files
copy "%APPPATH%\*.exe" "%APPPATH%\*.exe.deploy"
So I want to copy all .exe files to `.exe.deploy'
So if I have the following in a directory:
a.exe
b.exe
c.foo
d.bar
I want to end up with:
a.exe
b.exe
c.foo
d.exe
a.exe.deploy
b.exe.deploy
d.exe.deploy
There's got to be an elegant way of doing this. BONUS I'd also be like to specify multiple extensions (*.exe, *.txt, *.blob) and do it all in one command.
With PowerShell you'd enumerate the files you want to copy and pipe the results into the Copy-Item cmdlet:
Get-ChildItem $env:APPPATH -Filter *.exe |
Copy-Item -Destination { $_.FullName + '.deploy' }
Note that -Filter supports only a single string. If you want to pass multiple extensions you need to use -Include (but that works only in combination with -Recurse):
Get-ChildItem $env:APPPATH -Include *.exe,*.foo -Recurse |
Copy-Item -Destination { $_.FullName + '.deploy' }

path as parameter powershell

I have problem with path. I need to copy all files from directory I add as 1st parameter in Powershell command.
Now I have:
Copy-Item -Path "$args[0]/" -Filter*.*
So it will copy to location I am now (and it's ok, I don't want other location) but it doesn't copy anything. Please help.
Pretty sure the issue is that the 0 element in $args is not expanding in the string creating an invalid path. Wrap the variable in $() to allow it to expand inside a double quoted string. Else you would end up trying to copy the folder C:\TEST[0]/ which obviously is not correct.
Copy-Item -Path "$($args[0])/" -Filter *.* -Recurse
Not yet sure why you have a forward slash in there since Windows pathing uses backslashes.
function Copy-Something(){
test-Path "$($args[0])/"
test-path "$($args[0])"
}
Copy-Something C:\temp
With what little you have provided the output shows that it might be redundant to have the slash there. Would also recommend calling Test-Path on the argument anyway as it might have caught this for you.
True
True
From Question Comment
You are looking for the -Recurse parameter if you also want folder contents.
From Answer Comment
If you want the contents and not the folder you should be able to do something like this:
Copy-Item -Path "$($args[0])\*" -Filter *.* -Recurse
When in doubt, Get-Help Command.
PS C:\> Get-Help Copy-Item -ShowWindow
-------------------------- EXAMPLE 3 --------------------------
This command copies the contents of the C:\Logfiles directory
to the C:\Drawings\Logs directory. It creates the \Logs
subdirectory if it does not already exist.
Windows PowerShell
PS C:\> Copy-Item C:\Logfiles -Destination C:\Drawings\Logs -Recurse

How to use the Copy-Item cmdlet correctly to copy piped files

I am building a small script which should copy all .zip files to a special folder called F:\tempzip.
I tried it with Copy-Item cmdlet, but I didn't manage to do it. The script should copy all files from this folder (recursively) which are ".zip".
This is the part of the script I am talking about:
get-childitem F:\Work\xxx\xxx\xxx -recurse `
| where {$_.extension -eq ".zip"} `
| copy-item F:\tempzip
What do I have to add?
It's a lot simpler than that. Copy-Item has its own -Recurse switch. All you have to do is:
Copy-Item F:\Work\xxx\xxx\xxx\*.zip F:\tempzip -Recurse
When piping items to copy-item you need to tell it that "F:\tempzip" is the destination path.
| Copy-Item -Destination F:\tempzip
You can also cutout piping to the where operator by using Get-ChildItem's parameter -filter.
Get-Childitem "C:\imscript" -recurse -filter "*.zip" | Copy-Item -Destination "F:\tempzip"
Edit: Removal of unnecessary foreach loop and updated explanation.
For whatever reason, the Copy-Item recursion didn't accomplish what I wanted, as mentioned here, and how it is documented to work. If you have a bunch of *.zip or *.jpg files in arbitrarily deep subfolder hierarchies, and you want to copy them to a single place (one flat folder, elsewhere), I had better luck with a piped command involving Get-ChildItem. Say you are currently in the folder containing the root of your search:
Get-ChildItem -Recurse -Include *.zip | Copy-Item -Destination C:\Someplace\Else
That command will copy all the files and not duplicate the folder hierarchies.