PowerShell Commands

PowerShell Commands

PowerShell (Cmdlet) PowerShell (Alias) Description
Get-ChildItem gci, dir, ls Lists all files and folders in the current or given folder
Test-Connection[a] ping Sends ICMP echo requests to the specified machine from the current machine, or instructs another machine to do so
Get-Content gc, type, cat Gets the content of a file
Get-Command gcm Lists available commands
Get-Help help, man Prints a command’s documentation on the console
Get-WmiObject gwmi Gets instances of WMI classes or information about the available classes.
Clear-Host cls, clear Clears the Screen[b]
Copy-Item cpi, copy, cp Copies files and folders to another location
Move-Item mi, move, mv Moves files and folders to a new location
Remove-Item ri, del, erase, rmdir, rd, rm Deletes files or folders
Rename-Item rni, ren, mv Renames a single file, folder, hard link or symbolic link
Remove-Variable rv Deletes a variable and its value.
Get-Location gl, cd, pwd Displays the working path (current folder)
Pop-Location popd Changes the working path to the location most recently pushed onto the stack
Push-Location pushd Stores the working path onto the stack
Set-Location sl, cd, chdir Changes the working path
Tee-Object tee Pipes input to a file or variable, passing the input along the pipeline
Write-Output echo, write Prints Strings or other objects to the standard output
Get-Process gps, ps Lists all running processes
Stop-Service spsv Stops one or more running services
Stop-Process spps, kill Stops a running process
Select-String sls Prints lines matching a pattern
Set-Variable sv, set Create or alters the contents of an environment variable
Invoke-WebRequest iwr,curl,wget[f] Gets contents from a web page on the Internet
Invoke-RestMethod irm Sends an HTTP or HTTPS request to a RESTful web service

Introduction

PowerShell commands is a one of the best scripting platform and Command-Line Interface for Windows computers. A command-line interface (CLI) is a program for telling your computer to do jobs using your commands. It creates it possible to automate jobs and to do more things with one command. Mainly, the various tools of value to humanists can only be run from the command line.

It is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. Initially a Windows component only, known as Windows PowerShell, it was made open-souce and cross-platform on 18 August 2016 with the introduction of PowerShell Core. The former is built on the .NET Framework, the latter on .NET Core.

Added Key Points of PowerShell Commands

In fact, Microsoft has extended the capability of PowerShell script to Azure Cloud. One of the best automation service from Microsoft Azure is “Azure Automation”, it is used to automate frequent tasks on the Azure Cloud.

Powershell Commands Types

There are many types of powershell commands available like as “PowerShell,” “PowerShell ISE,” and “PowerShell.Right now we will use windows powershell. Just type powershell in your search bar and open the Windows Powershell.

Many persons think, PowerShell is a well alternative to Command Prompt because it has the more power and fast running tool. And another one main advantage of powershell uses   cmdlets rather than command prompt.

Powershell Commands
PowerShell

Difference B/W Cmdlets vs Commands

Cmdlets differ from commands in other command-shell environments in the following ways: Cmdlets are instances of .NET Framework classes; they are not stand-alone executables. Cmdlets can be created from as few as a dozen lines of code.

Cmdlets are written in a compiled .NET language, while functions (and scripts) are written in the PowerShell language. On the plus side, this makes certain developer tasks (such as P/Invoke calls, working with generics) much easier in a cmdlet.

Some of commands are working in both platforms (Powershell and Command Prompt) like as cd, ping, help, ipconfig, etc and powershell is centered on objects. All the data output from cmdlets is an object instead of text. This creates it easier for the user to navigate their way around difficult data.

How to Use PowerShell Commands

It is great tool for administrator who run a big and tangle management operation over a large network. You can simply run a script on PowerShell to automatically feed information from gather data about the multiple servers instead of manually

In PowerShell, you can run a variety of cmdlets, scripts, executables, and .NET classes and is most helpful to users who have earlier experience with command lines.

PowerShell Version in Windows

Windows 10                       – PowerShell 5 (built-in)

Windows 8 and 8.1          – PowerShell 4 (built-in)

Windows 7                          – Need to be install within .NET Framework

Cmdlets

Cmdlets are the main source to interact with Command Line Interface and it is comprised and structure to perform function is return to .NET object

Even all the cmdlets are written in C# programming language and more than 200 cmdlets can be used powershell, Windows powershell is not case sensitive method. Some important cmdlts are showing in below.

  • Get-Location – Get the current directory
  • Set-Location – Get the current directory
  • New-item – Create a new file
  • Rename – item Rename an existing file
  • Copy-item – Copy a file to a new location
  • Move-item – Move a file to a new location

You can view the Full list of cmdlets using the below Powershell commands.

get_Command

Customization of PowerShell

Microsoft is not allowed to users using custom powershell cmdlets in the default settings. If you want to use the powershell cmdlets, you need to modify the ExecutionPolicy from Restricted mode to RemoteSigned mode.

Once you had done this RemoteSigned feature after that you can run your own powershell script.

Step 1: Type in the following PowerShell commands

Steo 2: Refer the below snapshot for Access Denied. Make sure you’re on an Administrator account so that you have permission to set a new execution policy.

Customization of PowerShell

How to Run Scripts on Windows 10

Step 1: what is Script? It is a collection of commands or query saved into a text file, you should use .ps1 extension.               

Step 2: To create a PowerShell script using the Notepad editor, Open Notepad, write the simple query like “This is my First Script” and save that file “myscript.ps1”

Step 3: After creation of your script need to be you have to change the execution policy on Windows 10.

Step 4: Open the PowerShell in Run as Administrator mode.

Step 5: Type the following command to allow scripts to run and hit Enter:

Set-ExecutionPolicy RemoteSigned

Step 6: Type the following command to run the script and hit Enter:

.\my_script.ps1  Because my file is saved in E Drive

Powershell Commands
How to Run Scripts on Windows 10

Types of File Extension of PowerShell

  • PS1 – Windows PowerShell shell script
  • PSD1 – Windows PowerShell data file (for Version 2)
  • PSM1 – Windows PowerShell module file (for Version 2)
  • PS1XML – Windows PowerShell format and type definitions
  • CLIXML – Windows PowerShell serialized data
  • PSC1 – Windows PowerShell console file
  • PSSC – Windows PowerShell Session Configuration file

Backing Up an SQL DB

You can get to SQL Provider by opening the PowerShell ISE and running Import-Module SqlServer or by right-clicking on the Databases node of a SQL instance in Object Explorer of SSMS. Once here you can backup a single database by typing Backup-SqlDatabase and then the name of the database you want to backup.

Generating and maintaining backup are the most important tasks that any DBA has to fulfill. There are sevaral ways to backup a database, depending on company policies on disaster recovery.Most of the people use the Powershell to backup SQL DB.

The command-line interface can behavior full database backups, transaction logs and file backups. There are numerous ways to backup a database in PowerShell, the simplest command is  follow

Backup-SqlDatabase command.

For your Reference, the below command will create DB backup of DB with name of “Databsebkp”

PS C:\> Backup-SqlDatabase -ServerINstance “Computer\Instance” -Database “Databasebkp”

How to Get-EventLog

Log file is very important for all computer and applications. If you want to computer event log files using powershell. It is very simple open Powershell and type the below command

“Get-EventLog Security”

Get-EventLog

 List of Parameters available for you can search any eventlogs

  • After – User specifies a date and time and the cmdlet will locate events that occurred after
  • Before – User specifies a date and time and the cmdlet will locate events that occurred before
  • UserName – Specifies usernames associated with a given event
  • ComputerName – Used to refer to a remote computer
  • List – Provides a list of event logs
  • Index – Specifies index values the cmdlet finds events from

Get-Help

Powershell, “Get-Help” itself has parameters, and by adding -online to your Get-Help cmdlets.So in this page will explain to you for any doubt in cmdlets.

Powershell Commands
Get-Help

Final Words

The main concept of the PowerShell is Command Line Interface.It may have its individual unique cmdlets, but a treasure of online assets can help you with any administrative task you can think of.

It is very easiest method for new users. Powershell has a more than 200 cmdlets also it is most powerful application, if you are a new user in CLI working mode don’t worry more than very effective information are available in online.

Leave a Comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Scroll to Top