PowerShell

Copy-Item and Remove-Item: Automate copying and deleting operations

It can happen that files have to be deleted regularly or copied somewhere else. This is no problem in itself, as long as you know a little about scripting. But many people don’t get that far, they just buy the next product, hopefully not Total Commander, but “Super Copy”. The Ask Toolbar will then be installed at the same time. Later on, there is a lot of discussion about the vulnerabilities of Windows. Irony off.

PowerShell offers comprehensive Item cmdlets. I call them the Item Family.


Get-Command -Noun Item

1.PNG

In this article I would like to focus on two cmdlets: Copy-Item and Remove-Item.

Copy-Item

The command below copies a file C:\Temp\pwd.txt to C:\Temp1\.


Copy-Item -Path C:\Temp\pwd.txt -Destination C:\Temp1\ -Verbose

2.PNG

The verbose parameter can be omitted. It’s only there for showing what happens.

Copying folder and sub-folders

What about copying the entire content of directory including sub directories? For this, use the Recurse parameter.


Copy-Item -Path C:\Temp\ -Destination C:\Temp1\ -Recurse -Verbose

Copying files only

For copying only files without the folder and folder structure I can think of 2 examples.

Using Get-ChildItem with the File parameter and sending all the files through the pipe to Copy-Item.


Get-ChildItem C:\Temp\ -File -Recurse | Copy-Item -Destination C:\Tempnew\

Unbenannt.PNG

Or using Where-Object and specifying that folders, which means containers, should not be copied.


Get-ChildItem "C:\Temp\*" -Force -Recurse | Where-Object { -not $_.PSIsContainer } | Copy-Item -Destination C:\Tempnew\ -Verbose

Unbenannt.PNG

Copying files of a specific file type

Say, we want to copy only text files, that means all files with file extension txt. For this, we can use the filter parameter, that is included in the Copy-Item cmdlet.

The following command copies all text files, but not the folder itself. Note the asterix * after the folder name and the Filter.


Copy-Item -Path C:\temp\* -Destination C:\Temptxt -Filter '*.txt'

Unbenannt.PNG

Copying in Remote Sessions

Do you know PowerShell remote connections? Enter-PSSession and New-PSSession are your friends when it comes to remoting.  Two examples from me:

Copying all folders and files from C:\Temp on dc01 to C:\Temp on client01. Note that I’m logged on dc01. Remote Management on client01 is enabled (winrm qc).


Copy-Item -Path C:\Temp\ -Destination C:\ -ToSession (New-PSSession -ComputerName Client01) -Recurse

1.PNG

To copy back all those files use the FromSession parameter.


Copy-Item -Path C:\Temp\ -Destination C:\ -FromSession (New-PSSession -ComputerName Client01) -Recurse

More about Copy-Item here:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-6

Remove-Item

The cmdlet Remove-Item enables you to remove items, which means not only files, but also all other items. In PowerShell everything is an item and object!


Remove-Item -Path C:\Temp\hotfixes.htm -Verbose

To show you how to delete registry entries, I just want to take the example from the Remove-Item Help. This command deletes the OldApp registry keys.


Remove-Item hklm:\software\mycompany\OldApp -Recurse

To delete only txt files in a folder and it’s subfolders run


Remove-Item -Include *.txt -Recurse -Path C:\Temp1\ -Verbose

1.PNG

Automate Copy and Remove Jobs

Now let’s come to the point. As the headline says, I want to show you how to automate all this. Let’s assume we have the following scenario.

  • All files from a folder and its sub folder shall be copied as a backup to a destination folder
  • Afterwards the files in the source folder should be deleted. We should take care that the folder structure of the source folder remains untouched, that means that we must not delete the folder structure
  • The job has to be run every saturday on 11:00 am

This commands copies all files from C:\Original and it’s subfolders to C:\Destination.


Copy-Item -Path C:\Original\* -Destination C:\Destination\ -Recurse -Force

And this command deletes all files in C:\Original.


Get-ChildItem -Path C:\Original\ -Include *.* -Recurse | Remove-Item -Force

Unbenannt.PNG

Why not Remove-Item -Include *.* -Recurse? Well, I found out that there are some issues when combining filters and recurse:

Unbenannt.JPG

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-6

Back to topic. I now save the file as C: \Temp\OrigDest.ps1. It’s time to create a scheduled task that runs every saturday 11:00 am.


$Action=New-ScheduledTaskAction -Execute 'powershell' -Argument 'C:\Temp\OrigDest.ps1'
$Trigger=New-ScheduledTaskTrigger -Weekly -DaysOfWeek Saturday -At 11am
$Set=New-ScheduledTaskSettingsSet
$Principal=New-ScheduledTaskPrincipal -UserId 'sid-500\administrator' -LogonType S4U
$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Set -Principal $Principal
Register-ScheduledTask -TaskName "Copy Orig to Dest" -InputObject $Task -Force

Unbenannt.PNG

That’s it. Automation completed 😉

6 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.