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.
- 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.
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.
You can view the contents of a folder by using a command called DIR. Use this to view folder created before.
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.
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.
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.
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
Post a Comment