What is PowerShell ?
PowerShell is a command-line shell and a scripting language all in one. It was designed as a task engine that uses cmdlets to wrap that people need to do. In Powershell, you can run commands on local or remote machine. You can do tasks like managing users and automating workflow.
Powershell consists of two parts: a command -line shell and a scripting language. It started out as a framework to automate administrative tasks in Windows.powershell has grown into a cross-platform tool That’S used for many kinds of tasks.
A command-line shell lacks a graphical interface, where you use a mouse to interact with graphical elements. Instead, you type text commands into a computer console. Here are some of Benefits of using a console:
- Interacting with a console is even faster than using a graphical interface.
- In console, you can run botches of commands, so it’s ideal for task automation for continuous-integration pipelines.
- You con use a console to interact with cloud resources and other resources.
- You con store commands and scripts in a tent file and use a source-control system. This capability is one of the biggest benefit, because your commands are repeatable and auditable. In many systems, specially government systems, everything must be traced and evaluated a kaudited. Audit Cover everything
Features
PowerShell share some features with traditional shells.
- built-in help system-PowerShell offered a comprehensive help system that provides detailed information about commands and their pacesetters. This system often links to online resources for
in-depth learning, making it easy for users to understand and utilize PowerShell effectively.
- Pipeline – one of the core concepts of PowerShell is pipeline.it allows for chaining multiple commands together, where the output of one command becomes input of the next. Unlike traditional shells that Operate on text,Power Shell works with objects, enabling more powerful and flexible operations.
- Aliases – to facilitate a smooth transition for users family with other shells, PowerShell supports aliases. These are alternative names for common commands, such as cls(clear screen) and ls(list files). By using familiar aliases, users can quickly adapt to PowerShell and leverage its capabilities.
PowerShell differs from a traditional command-line shell in few ways:
- It operate object over text -PowerShell uses objects as input and output. Reducing the need for formatting and data extraction. This makes scripting more efficient compare to tradition shells that rely on tent based input and output.
- PowerShell uses cmdlets – PowerShell uses cmdlets, Built on a common runtime, For consistent behaviour they typically take object input and return object output. Core cmdlets are built in .NET core I and open-source. PowerShell can be extended with custom cmdlets, scripts, and functions.
- It has many types of commands – PowerShell commands can be native executables, Cmdlets, functions, script, or aliases. Cmdlets are a type of command built on a common runtime.
Difference between PowerShell and CMD.
What is command prompt?
Command prompt is a command is a command line shell, which is based on 1980s
Microsoft Disk operation system (MS-DOS). It is a simple win32 application that helps users to interact with the Windows operating system using text-based commands and parameters.
Command prompt has built-in commands to run various tasks, most of which can be executed as an administrator.
Command prompt is officially known as Windows Command Processor, but sometimes it is also referred to as the CMD prompt or command shell, or even by its filename, cmd.exe.
What is powershell ?
PowerShell is a more advanced version of the command prompt. It is used to execute external programs like ping or copy, and automate different system administration tasks which are not accessible from cmd.exe.
It is an automated task-based command-line shell and associated scripting environment
Designed for system administrators.it is build on .NET framework.
windows PowerShell ISE
Microsoft Windows PowerShell ISE is a graphical user interface-based application and a default editor for Windows PowerShell. ISE stands for the Integrated Scripting Environment. It is an interface in which we can run commands and write, test, and debug PowerShell scripts without writing all the commands in the command-line interface.
ISE is a tool that makes coding easier It helps you write code faster by suggesting code, highlighting mistakes, and provided helpful tips.
Windows PowerShell ISE is a part of Microsoft Management framework, which allows administrators for managing the multiple version of windows and windows server. It allows us to execute commands in the console pane.
The integrating scripting environment ( ISE) was first introduced with version 2.0 of windows
PowerShell and was re-designed with power-shell version 3.0.
Key features
There are the key features of windows PowerShell ISE:
- Multiline editing:- in command pane, to insert a blank line under the current line, press SHIFT + ENTER.
- Selective execution: – to execute a part of script, select the tent to run, and click the – Run script or, press the F5 key
- Context-sensitive help: Type the Invoke-Item and then press the f1 A help file opens to the article for an Invoke-Item command let.
The ISE window of PowerShell consists of following three panes:
- Script Pane: This pane allows the users to create and run the scripts. A user can easily open, edit, and run the existing scripts in the script pane.
- Output Pane: This pane displays the outputs of the scripts and the commands which are run by you. You can also clear and copy the contents in the Output pane.
- Command Pane: This pane allows the users to write the commands. You can easily execute the single or multiple-line command in the command pane.
PowerShell Core
PowerShell version 5.1 is the latest release of original PowerShell, but starting with PowerShell version 6.0, it is now referred to as a PowerShell core.
PowerShell Core is an open-source and available on the Windows, Linux, and Mac operating systems. It is built on the .NET core.
PowerShell core is launched as pwsh on Linux and MacOS and pwsh.exe on Windows.
Differences Between PowerShell and PowerShell Core
Installation
If you’re running Windows 8 or later, a version of PowerShell called Windows PowerShell should already be installed. This version differs slightly from the most up-to-date PowerShell release, but it works fine for learning purposes.
You can open Windows PowerShell from the Start menu.
If you use windows 8 or later, a version of PowerShell called windows powershell should already be installed.
If your computer is using something else than windows 8 or later, or Computer that runs Linux, macOS or a older version of windows. You need to install PowerShell. You can follow the link to download. – https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.4
If you using visual studio code, you con also use PowerShell extension for visual studio code. This extension lets you run commands, and also helps you with snippets, code completion, and syntax highlighting.
Before Running Power-shell Scripts
By default windows wont allow you to run scripts by simply opening it, because malicious script can damage your computer.
To run power-shell scripts you need to change execution policy. To change it you should run the following command
Get-ExecutionPolicy
You will see one of the following output:
- Restricted – No script will execute. This will be default so you need to change it.
- AllSigned- you can only run script signed by trusted developer
- RemoteSigned – you can run your script on script signed by trusted developer.
- Unrestricted – you con run any swift you want.
To start working with PowerShell Script you need to change the execution policy. You should change to ‘ RemoteSigned’. You can change by using command
Set-ExecutionPolicy RemoteSigned
Powershell script
Create a PowerShell script
On Windows, a user can create the PowerShell script using any text editor or the Integrated Scripting Environment (ISE), which is already available on every system.
Create a script using notepad
Open notepad. Type the swift you want to execute for example. Echo “hello world… “
Now save this file with .ps1 format.
Create a script using the integrated scripting environment.
The following steeps are used to create a PowerShell script using the ISE tools:
- Open start
- Search for the Windows PowerShell ISE, right-click on the ‘Windows PowerShell ISE‘ and select the ‘Run as Administrator‘ option from the drop-down list.
- Click on the File in the menu bar.
- Select the ‘New’ option to create a new empty file.
- Write a script which you want to execute. For example: Echo ”Hello world…..”
Run a PowerShell script
To execute the powershell script from the command line. Follow given steps.
- Open windows powershell
- Type the location of your script file – cd (location_of_file). Then press enter
- Then type – .(file_name).ps1
PowerShell Comments
Comments are used to provide the information about the code . Just like other
Programming languages you con also use comments in powershell for documentation purpose.
There are two types of comments
- Single-line comments
- multi-line comments or comment block
Single line comments
Single line comment refers to the description in a single line. Use # symbol at the starting of the line and everything offer it inside the single line will be ignored and will become comment.
syntax
< any command on statement > # < any comment >
or
# <any comment >
< any command or statement>
example
PS C: >get-children # this command displays the child item of C: drive.
Multi line comments
With PowerShell 2.0 or above, multiple line comments or block comments have been introduced. To comment the multiple lines, put the <# symbol at the beginning of the first line and #> symbol at the end of the last line.
syntax
<# Multiple line Comment………
………
………………..#>
Statement-1
Statement-2
Statement-N
Example
- PS C:> <# This code is used to print the
- >> factorial of a given number#>
- PS C:> $a=5
- PS C:> $fact=1
- PS C:> for ($i=$a;$i -ge 1;$i–)
- >> {
- >> $fact=$fact * $i;
- >> }
visit LinkedIn – Click here
Read more Blogs – Click here