POWERSHELL, How To Use Powershell?
We can operate the computer by two methods first command-line interface and a Graphical User Interface. Here I shall discuss CLI.
In Command-Line Interface CLI, there are two methods we can use to interact with computers.
- Command Prompt
- Powershell
In this article, We will know about Powershell.
What is Powershell?
Powershell is open-source software that runs on all three operating systems (Windows, Linux, Mac OS). It is used to automate the management system. It is developed in 2006 for admin.
Powershell provides the facility for scripting. It is an advanced form of CMD. Powershell is the combined form of CMD, .NET, and C#.
Windows follows the path to show something like:
c:\users\desktop\myfile.txt
So, windows use this type of path to jump into other directory or folder.
In windows, we use backslashes (\) but in Linux, we use forward slashes (/)
C:\ is called the root directory of C.
A:\ is called the root directory of A. In the same way, if you have D volume in the computer, the root directory is D:\.
Root Directory
It is the top level of the hierarchy. It contains a parents and child directory. The root Directory folder is called the Parent Directory and the Child Directory is inside the Parent Directory.
How to start the Powershell in Windows?
Go to Run by Clicking Windows + R.
It is opened with a blue screen.
or
Search in taskbar 'Powershell'.
Starting With Powershell
List of Directory
ls
It is used to show the list of directories.
Suppose we want to know about the list in C drive. We use it like this.
PS C:\> ls and press enter (↵)
Here c:\ represents the root directory. > represents Powershell mode and you can start to go.
Get-help ls
This describes the ls parameters and aliases of ls i.e. gci, ls, dir. These are the same. You can use gci or ls or dir in place of ls. Alias is a nickname for command.
ls -force
It is used to display the hidden files.
Get-Childitem
It is also used for listing the items in the directory. It does the same thing as dir, ls, and gci.
Clear-Host
It is used to clear the screen. In place of it, you can also use the cls command.
Clear
It also does the same thing as cls or clear-Host.
How To jump into another directory?
To jump into the Directory, we use cd i.e. Change Directory
cd
Suppose you are in a current directory like users and want to go desktop directory then you can use cd like this.
PS C:\Users> cd c:\users\Asus\desktop\ and hit enter.
Use of tilde (~)
To jump into a directory like Desktop, a tilde is very helpful. It helps to write a path in shortcuts.
cd ~\Desktop
Only a small path helps to jump into the desktop directory.
You will use tilde and backslashes (~\)
Tab
It helps to complete the path of the directory. After pressing the tab if the path is not completed, you are entering the wrong command.
Suppose we use cd ~\desk and hit tab. You will see a complete path of the desktop directory like this C:\Users\Asus\Desktops\.
pwd
PWD Stands for Print Working Directory. It is used to know about the current directory in which you are working.
mkdir
To create a folder, we use mkdir (make directory) and then the name of the folder. Here we will create a folder on the desktop.
PS C:\Users\ASUS\Desktop>mkdir my_file and hit enter
To see the folder, type ls like this
PS C:\Users\ASUS\Desktop> ls
new-item
To create a file, we use the new-item command. Here, we create a file documents.txt in the folder my_file.
PS C:\Users\ASUS\Desktop\my_file>new-item documents.txt -- to create a text files
PS C:\Users\ASUS\Desktop\my_file>new-item word.xlsx -- to create a excel files.
To access the files or folders within the directory we can use dot and backslash(.\) then file or folder name or you can use the full path to access these items.
rm
To remove the files and folder, we use the rm command.
PS C:\Users\ASUS\Desktop>rm .\documents.txt and hit enter --to remove files
PS C:\Users\ASUS\Desktop>rm .\book\ and hit enter --to remove the folder
History Command
It returns all commands that you already used in the Powershell.
PS C:\Users\ASUS\Desktop>history and hit enter.
Copy the Files and Directory, How to use the CP command?
To copy the files and folder, we use cp.
syntax:
PS C:\Users\ASUS\Desktop>cp (files or folder_name) (path_name)
Suppose we have a file abc.txt on the desktop and we want to copy and paste it into the my_file folder.
PS C:\Users\ASUS\Desktop>cp abc.txt c:\Users\Asus\Desktop\my_file\
We can also copy all files or folders at a time and paste them into another folder. For that, we use wildcard Asterix (*).
Suppose we use *.txt which means all text files in the folder. If we write *.xlsx which means all excel files in the folder and so on.
PS C:\Users\ASUS\Desktop>cp *.txt c:\Users\Asus\Desktop\my_file\ Here copy all txt files from Desktop and paste into my_files folder.
How to move the files and folder?
The command to move the files and folder is mv. We use the mv command in the same way as cp.
syntax:
PS C:\Users\ASUS\Desktop>mv (files or folder_name) (path_name)
To move one file
PS C:\Users\ASUS\Desktop>mv abc.txt c:\Users\Asus\Desktop\my_file\
To move all files at a time, we use asterix
PS C:\Users\ASUS\Desktop>mv *.txt c:\Users\Asus\Desktop\my_file\
How to change file name and directory name?
To change the file name or directory name, we can also use the mv command.
Syntax:
PS C:\>mv (files or folder_name) (new_name)
PS C:\>mv abc.txt xyz.txt --To rename the abc.txt to xyz.txt.
In the same, you can rename the folder. Suppose we have a folder named abc and want to change it xyz. For that, we type the command like this.
PS C:\>mv abc xyz
How to write the content in files through Powershell?
To write the content in the document in PowerShell, We use the command add-content or set-content.
Set-content
It is used to add the content to the file. It adds the text by replacing the previous text.
Add-content
It is used to add the content to the file without replacing the previous content.
syntax:
PS C:\Users\ASUS\Desktop>set-content (file_name) ("write the content in single or double quotes") --With replacing
PS C:\Users\ASUS\Desktop>add-content (file_name) ("write the content in single or double quotes") --Without replacing
Example:
PS C:\Users\ASUS\Desktop>add-content .\abc.txt 'Hello Powershell'
PS C:\Users\ASUS\Desktop>add-content .\abc.txt "Hello World"
Result:
Hello Powershell
Hello World
If we are using set-content like this,
PS C:\Users\ASUS\Desktop>set-content .\abc.txt 'This is a text file.'
Result:
This is a text file.
Set-content replaces all previous content sets with the current content.
How to read the content from the text file in Powershell?
To see the content of the file in Powershell, we use the cat (concatenate) command. For that write the command.
PS C:\Users\ASUS\Desktop>cat .\abc.txt
or
PS C:\Users\ASUS\Desktop>cat C:\Users\ASUS\Desktop\abc.txt
How to delete the content from the file in Powershell?
To clear the content from the file, we use the command clear-content.
PS C:\Users\ASUS\Desktop>clear-content C:\Users\ASUS\Desktop\abc.txt
Comments
Post a Comment