Important Commands In PowerShell

Introduction 

 
As per the Microsoft documentation, "PowerShell is a cross-platform task automation and configuration management framework, consisting of a command-line shell and scripting language. Unlike most shells, which accept and return text, PowerShell is built on top of the .NET Common Language Runtime (CLR) and accepts and returns .NET objects. This fundamental change brings entirely new tools and methods for automation."
 
While working on PowerShell there are few basic but important commands which make life easier. I tried to list and explain them below.
 

Get-Location 

 
This command is used to get the location of the current Directory.
 

Write-Host

 
This command is mainly used to display or print the output, it can be text, message, variable value, or anything that the developer wants to print. you can pass some parameters to print in a specific format. For example, you can use ForegroundColor to print in specific color or BackgroungColor to print output in the specific background color. Below is the sample code to understand more.
  • Write-Host"I am using CSharp Corner to share my knowledge" -ForegroundColor red -BackgroundColor white

Get-Content

 
This command is used to get the text or content from the file or from the function. you need to specify the path of the file in case of getting the contents from the file.
  • Get-Content -Path .\Samplefile.txt - This command read all the content of the file with all formatting
  • Get-Content -Path .\Samplefile.txt -Raw - This command read all the content of the file without formatting. it will return all content in a single line

Set-Content

 
This command is used to write content to the file.
  • Set-Content -Path .\Test.txt -Value'Hello World'

New-Item

 
This command is used to create a new item, file, or directory at a specified location or path. If you are creating the file with this command you can write content in the file with the same command
  • New-Item -Path"c:\" -Name"testfile1.txt" -ItemType"file" – This create only file with empty content
  • New-Item -Path"c:\" -Name"testfile1.txt" -ItemType"file" -Value"This is a text string." – This command create file with content
  • New-Item -Path"c:\" -Name"TestFolder" -ItemType"directory" – This command creates folder
  • New-Item -ItemType"directory" -Path"c:\ps-test\scripts" – This command creates folder inside different folder or path
  • New-Item -ItemType"file" -Path"c:\ps-test\test.txt", "c:\ps-test\Logs\test.log" – This is the way you can create multiple files at a time
  • New-Item -Path .\TestFolder -ItemType Directory –Force – With the help of FORCE parameter you can override the existing folder

Copy-Item

 
This command copies an item from one location to another. This command doesn’t cut or delete any file. It can copy and rename in the same command.
  • Copy-Item"C:\TestFolder\test.txt" -Destination"C:\NewFolder" – with this command copy specified file to the folder
  • Copy-Item -Path"C:\Logfiles\*" -Destination"C:\Drawings" –Recurse – with this command copy all files to specified folder
  • Copy-Item -Path"C:\Logfiles" -Destination"C:\Drawings\Logs" –Recurse – Copy directory and all content to specified folder

Compress-Archive

 
Creates a compressed archive, or zipped file from specified files and directories.
  • Compress-Archive -Path C:\TestFolder -DestinationPath C:\Archives\Draft.zip - Compress a directory that includes the root directory
  • Compress-Archive -Path C:\Reference\* -DestinationPath C:\Archives\Draft.zip - Compress a directory that excludes the root directory
  • Compress-Archive -Path C:\Reference\*.* -DestinationPath C:\Archives\Draft.zip - Compress only the files in a root directory

Expand-Archive

 
Extracts files from a specified archive (zipped) file.
  • Expand-Archive -LiteralPath'C:\Archives\Draft.Zip' -DestinationPath C:\Reference – With this command extract the contents of an archive
  • Expand-Archive -Path Draft.Zip -DestinationPath C:\Reference – With this command extract the contents of an archive in the current folder
There are many important commands available, however, I used the above command frequently while working on PowerShell scripting. As a developer, we can do almost everything after application development with the PowerShell. This scripting is an add-on to the skills of the developers, even POC knowledge is also adds huge capability.
 
--Happy Coding--


Similar Articles
Blue Ocktopus Technology Systems Private Limited
Enabling revenue generation through brand loyalty and customer retention for enterprise retail and e