Copy, Move and Delete files with PowerShell

In this article, let me show you how you can use PowerShell to copy, move and delete files. Before we go through these commands, let me show you how to navigate between different directories/folders.

How to change the directory (folder) in Command Prompt (CMD)


CD (Change Directory): This command enables you to change the current directory. 
  • When you need to go one folder up, use the "cd.." command. Let’s assume that you want to go back to the Windows folder. Type “cd..” and press Enter on your keyboard.

  • cd takes you to the top of the directory tree when you pass "\" after it. In this case, to the “C:” drive.
  • If you need to go to a specific folder from this drive run the command "CD Folder". for example, When you need to access the cstutorials folder located in "C:" type "cd cstutorials" as shown below, and then press Enter on your keyboard.

CD cmdlet is alias for set-location. you can use set-location also.



How to create a new directory with Command Prompt (CMD)


You can make a new folder using the md command. The syntax of these commands is "md folder".

here we are creating folder test1 using md as shown below.



md is an alias for mkdir. You can use mkdir also to create new directory.



How to view the contents of a directory in Command Prompt (CMD)


You can view the contents of a folder by using a command called DIR. Use this to view folder created before.


How to create a new file with PowerShell command


New-Item command creates a file that is named "logs1.txt" in the current directory. Value we pass along with -itemtype decide whether new item is file or directory.



How to delete file or folder with PowerShell command


Remove-Item command is used to delete files or folder. rmdir is an alias for remove-item. We have deleted logs1.docx file created using new-item command below.


just like rmdir, you can use remove-item to delete files.


You can delete everything from folder, pass * after remove-item as parameter. As cstutorials folder have test1 and test2 folder which further have files within them. It will ask for user response.


How to copy items from one location to another location using PowerShell


With copy-item command, you can copy files from one location to another.

Here we have copied file logs1.txt using copy-item from test1 folder to test2 in first line. In second line, we have copied same logs1.txt file and at the same time renamed it by passing not just path in destination but also a different name.


How to move files from one location to another location using PowerShell


The Move-Item cmdlet moves an item, including its properties, contents, and child items, from one location to another. It can also move a file or subdirectory from one directory to another location.


As I'm in test1 folder, it will look for logs1.txt in test1 folder. You can provide complete path with file name here. In destination you can pass only path or different file name also if you want to rename it.



YouTube video link:




Comments

Popular Posts

PowerShell Arithmetic Operators

How to Import and Export Delimited Files, like CSV, in PowerShell

How to generate a GUID in PowerShell