Basic File System commands in Powershell

Below is the list of file commands in Windows Powershell.

Displaying the contents of file

 “cat” command is used to display the contents of file. Get-Content, gc, type – all of these are same commands with different names.

Here is the syntax of cat command.

> cat <file-name>

Deleting entire data from a file

To make the file empty, we can use “Clear-Content” (also called as clc) command.

> clc <file-name>

Creating a directory using mkdir (New-Item) command 

> mkdir <directory-name>

Deleting a file or a directory using rm (Remove-Item) command 

> rm  <file-name-or-directory-name>

To delete the files recursively in a directory, you can use below command.

> rm  -r <directory-name>

Copying files using cp (Copy-Item, cpi, copy) command

Here is the syntax of cp command.

cp <source-file> <destination-file>

Moving files using mv (Move-Item, mi, move) command

You can use below command to rename a file.

$ mv <old-file> <new-file>

You can use below command to move a file.

$ mv <source-file> <destination-file>

You can use below command to move a file forcefully.

$ mv -fo <source-file> <destination-file>

Rename item

Rename-Item <original-file-name> <new-file-name>

Listing files and directories using ls (dir, gci, Get-ChildItem) command.

“ls” command shows the list of file and directory names in the current directory. But You can also specify the names of directories explicitly.

$ ls dir1, dir2

Resolving paths

You can use below command to find out the absolute path of the specific files.

> Resolve-Path <file-name>

Check if file exists

You can use below command to check if specific file exists. This will display output as true if the specified file exists.

Test-Path “f1.*”

Display and set present working directory

You can use “pwd” (Get-Location, gl)command to display present working directory.

To change the working directory , you can use “cd” (chdir, sl, Set-Location) command.

Set-Location <directory-name>

pushd (Push-Location) and popd(Pop-Location) commands in Powershell

These commands are used to switch to multiple directories with ease.

 

You may also like...