What is PowerShell? Here's a Complete Explanation
What is PowerShell? PowerShell is a type of command-line interface that supports object-oriented programming techniques in Windows. PowerShell is often used by administrators working with Windows Server operating systems to run various network setup routines on Windows-based servers. In addition to providing API (Application Program Interface) features that can be accessed by other applications, PowerShell is actually a .NET framework output product that can be executed on several types of OS that produces output in .text and .net formats.
Difference between PowerShell and CMD
The main difference between PowerShell and CMD (Command Prompt) is that commands known as cmdlets are useful for managing the registry to Windows Management Instrumentation (WMI) settings. Because it is similar to a programming language, PowerShell can perform a more complex set of commands than CMD.
PowerShell Features
The features in PowerShell are very useful for network administrators when working with Windows-based servers, including the following.
1. Scripting
This feature supports object-oriented programming mode, making it easy for other applications to access.
2. Cmdlets
This feature supports server configuration settings such as running services, process management, logs, and registry management.
3. Simplified
The consistency of PowerShell feature calls in the command line with GUI navigation features makes it easy for users to remember.
4. Powerful object manipulation capabilities
Accessed and configured objects can be manipulated via PowerShell or Windows GUI tools.
5. Extensible Interface
Applications or software developed by vendors can access and take advantage of the PowerShell API features
Things To Face In PowerShell
1. Get to know PowerShell
A. Run PowerShell, click the Windows PowerShell icon on the taskbar or click Start – Search – type PowerShell
B. To display the command prompt list help, type help * then Enter.
C. Press enter to prompt a help list or a space to enter the next page. Press the Q key to exit help mode.
D. Type the Get-Command Help command to view the Get-Command cmdlet command help information
PS C:\Users\Administrator>Get-Command Help
E. To have the Get-Command cmdlet command details displayed on the screen, type the command.
PS C:\Users\Administrator>Help Get-Detailed-Command
F. Detailed information content of Get-Command cmdlet can be displayed by command
PS C:\Users\Administrator>Get-Command -Full Help
G. The Get-Help About command is very helpful for administrators in finding and displaying a list of commands that can be used in PowerShell.
2. View List of PowerShell Commands
A. To display a list of commands available in PowerShell, type the following command.
PS C:\Users\Administrator>Get-Command
B. Actually, you don't need to memorize one command at a time in PowerShell, just type a few first characters then press Tab, the system will automatically display the fixed command. For example the command Get-C, then press Tab. PowerShell will fulfill these commands automatically, you just need to choose the command that suits your needs. Press Tab if you haven't found the right command, if you have, press the Enter key.
3. Format In Filtering Output
In this step, you will learn how to use command parameters and cmdlets to filter data. To display a list of services installed on the computer, type the command.
PS C:\Users\Administrator>Get-Service
The commanding feature in PowerShell works by utilizing services ranging from Get, New, Restart, Resume, Set, Start, Stop, and Suspend. To display this feature type the command Get-Command -Noun Service
4. Various PowerShell Commands With Their Functions
Command - Function
Get-Service Spooler
To display a list of service spoolers
Get-Service M
To display a list of all services starting with the letter M
Get-Service M* | Format-List
To display a list of all services starting with the letter M with detailed information for each service
Get-Service M* | Format-Custom
To display a list of all services starting with the letter M complete with detailed information on each service that is run with a different display from Format-List
Get-Service M* | Where-object ($_.Status -eq “Running”}
Displays a list of running services starting with the letter M
Information:
a. Where-Object, command to filter input
b.{}, separate script code in PowerShell
c. $_ is an input reference object )all services starting with the letter M)
d. -eq is a parameter specification, in this case it will compare the service status of the service in the running condition
Get-Service M* | Where-object ($_.Status -eq “Stopped”}
Displays a list of services in a stopped state starting with the letter M
Get-Service | Sort-Object Status
Displays a list of services in the machine complete with their status
Get-Service | Sort-Object Status | Format-Table _GroupBy Status Name, DisplayName
Displays a list of services in the machine complete with their status grouped by each service
5. Displaying Object Meta
Each PowerShell object is basically .NET based which refers to cmdlets, services, and processes that aim to simplify server administration. Here's a list of commands that can be used to display object metadata information in PowerShell:
a. Get-Service | Get-Member is used to view the metadata type of each object generated by the Get-Service command.
b. Get-Process | Get-Member is used to display the object's metadata type generated from the Get-Process command.
c. To create a new object of type System.Diagnostics.Process and display its metadata, type the command
PS C:\Users\Administrator>New-Object System.Diagnostics.Process | Get-Member
d. Type the command Get-Service Spooler | Select-Object ServiceDependedOn to display a list of spooler services that have depended on the state.
6. Using the Show Command
PowerShell provides a graphical help feature to display detailed information for a command.
a. Use the Show-Command command to display the help window.
b. To further filter information about a command, use a command with the Show-Command [cmdlet] format, for example.
PS C:\Users\Administrator>Show-Command Get-Service
c. The output list of services running on the server can be shown with the command Get-Service -ComputerName Localhost.
7. Whatif and Confirm Commands
PowerShell also gives network administrators permission to safely test and learn commands in PowerShell (performing simulations). In this exercise, we will explain how to use the whatif and confirm commands.
a. The whatif command is used to simulate or display the effect or impact produced when executing a particular command in PowerShell. For example, to display a list of services (starting with the letter M) that will be stopped when executing the Stop-Service command, use the Stop-Service M* -whatif command.
b. Confirm command will give a warning about the impact of the executed operation. An example of a Confirm command is Stop-Service M* -Confirm. At the command, PowerShell will ask whether the command will be executed or not, press Y to continue8. Creating Variables In PowerShell
As in programming materials, variables are temporary storage memory to hold certain data before being reused. Here are the steps you need to know.
a. Create a variable with a data value of type string see the following example.
PS C:\Users\Administrator>$text = “Hello, this is a powershell string variable”
b. How to display the contents of a text variable is with the command.
PS C:\Users\Administrator>$text
Hello, this is a powershell string variable
PS C:\Users\Administrator>Write-host $text
Hello, this is a powershell string variable
By default, the value of each variable in PowerShell is Null which is represented by $null.
c. Each variable in PowerShell can be stored in a specific location, then to display a list of variables that have been created and stored in a specific directory, you can use the Get-Variable command.
d. Create a variable with a numeric data type, not using quotation marks, for example.
PS C:\Users\Administrator>$number = 12000
e. In addition to numeric, PowerShell variables can also store array data types, for example.
PS C:\Users\Administrator>$array = 4,5,7
f. To see the new type in a variable, you can use the command.
PS C:\Users\Administrator>$number.GetType() . FullName
g. To measure the length or number of array data in a variable, you can use the length option, for example as follows.
PS C:\Users\Administrator>$array.Length
h. To call the 2nd array data value in a variable, you can use the following command.
PS C:\Users\Administrator>$array[2]
i. Data values in arrays that were created in the previous variable ($array) can be re-declared with an integer type, for example.
PS C:\Users\Administrator>[int[]] $array = (4, 5, 7)
j. If in an array variable ($array) a data value has been given, then you insert a new data value that is different from the previous one, it will produce an error.
PS C:\Users\Administrator>$array[1] = “this is a string”
9. String Operations In PowerShell Variables
a. Concatenate two strings using the plus sign (+)
PS C:\Users\Administrator>$text1 = “Monitor”
PS C:\Users\Administrator>$text2 = “Technology”
PS C:\Users\Administrator>$text1 + $text2
b. To count the number of characters in a previously concatenated string, you can use the string function in the .NET framework.
PS C:\Users\Administrator>($text1 + $text2) . Length
c. In PowerShell, also recognize binary operations (same as Linux shell) that can be used to perform comparisons between two data. The following table functions of some operators.
d. The decimal type data value format can also be set in PowerShell. For example, changing the value 17.5 to 17.50.
PS C:\Users\Administrator>”{0:f2}” -f 17.5
e. In order to convert the decimal data type to a currency type with a distance of 15 characters from the left, you can use the following command.
PS C:\Users\Administrator>” | {0,10:C}” -f 17.5
f. You can also see the clock or running time in PowerShell with the following command.
PS C:\Users\Administrator>”{0:hh:mm}” -f (Get-Date)
10. Create a Script File
Script files are files used to store some PowerShell commands for the purpose of executing those instructions. There are four kinds of rules that describe how PowerShell executes the script, as follows.
Restricted, PowerShell cannot run script files, so the user must execute instructions through interactive mode.
AllSigned, a script file that has been defined and created by a legitimate user (developer) can be run in PowerShell.
RemoteSigned, the downloaded script file must come from a legitimate (trusted) developer.
Unrestricted, there are no special requirements to run a crypt shell.
a. The first step, you check the type of policy (rules) in PowerShell first with the Get-ExecutionPolicy command.
PS C:\Users\Administrator>Get-ExecutionPolicy
b. Next set the value to Unrestricted.
PS C:\Users\Administrator>Set-ExecutionPolicy Unrestricted
PS C:\Users\Administrator>Get-ExecutionPolicy
c. Right-click the Windows PowerShell icon – select Windows PowerShell ISE.
Windows PowerShell ISE is a GUI-based host application that makes it easy for users to create, write, run shell scripts, test, and debug them. In addition, support for editing lines of code, using the Tab key feature, colored lines of code, and support for help will make it easier for users to execute them.
d. Click the File menu - select New or press Ctrl + N and write a script as shown below.
. Save it with the name trial.ps1 in the C:\Users\Administrator\Documents folder.
f. In the PowerShell section of the Windows PowerShell ISE window, type the following command.
PS C:\Users\Administrator>cd C:\Users\Administrator\Documents
g. To execute the trial.ps1 file use the .\trial.ps1 command.
h. In addition to using the .\trial.ps1 command you can also use the invoke-expression and & instructions.
Summary
So What Is PowerShell? PowerShell is an automated framework from Microsoft, with a command-line shell and scripting language integrated into the .NET framework. PowerShell also has features like Scripting, Cmdlets, Simplified, Powerful object manipulation capabilities, Extensible Interface.
Hopefully, this article about What is PowerShell gives you a little insight. Also, read an article about What is PPPoE on a router that you may need to know. Thank you.