PowerShell – Scripting

Powershell – Scripting ”; Previous Next Windows PowerShell is a command-line shell and scripting language designed especially for system administration. Its analogue in Linux is called as Bash Scripting. Built on the .NET Framework, Windows PowerShell helps IT professionals to control and automate the administration of the Windows operating system and applications that run on Windows Server environment. Windows PowerShell commands, called cmdlets, let you manage the computers from the command line. Windows PowerShell providers let you access data stores, such as the Registry and Certificate Store, as easily as you access the file system. In addition, Windows PowerShell has a rich expression parser and a fully developed scripting language. So in simple words you can complete all the tasks that you do with GUI and much more. Windows PowerShell Scripting is a fully developed scripting language and has a rich expression parser/ Features Cmdlets − Cmdlets perform common system administration tasks, for example managing the registry, services, processes, event logs, and using Windows Management Instrumentation (WMI). Task oriented − PowerShell scripting language is task based and provide supports for existing scripts and command-line tools. Consistent design − As cmdlets and system data stores use common syntax and have common naming conventions, data sharing is easy. The output from one cmdlet can be pipelined to another cmdlet without any manipulation. Simple to Use − Simplified, command-based navigation lets users navigate the registry and other data stores similar to the file system navigation. Object based − PowerShell possesses powerful object manipulation capabilities. Objects can be sent to other tools or databases directly. Extensible interface. − PowerShell is customizable as independent software vendors and enterprise developers can build custom tools and utilities using PowerShell to administer their software. Variables PowerShell variables are named objects. As PowerShell works with objects, these variables are used to work with objects. Creating variable Variable name should start with $ and can contain alphanumeric characters and underscore in their names. A variable can be created by typing a valid variable name. Type the following command in PowerShell ISE Console. Assuming you are in D:test folder. $location = Get-Location Here we”ve created a variable $location and assigned it the output of Get-Location cmdlet. It now contains the current location. Using variable Type the following command in PowerShell ISE Console. $location Output You can see following output in PowerShell console. Path —- D:test Getting information of variable Get-Member cmdlet can tell the type of variable being used. See the example below. $location | Get-Member Output You can see following output in PowerShell console. TypeName: System.Management.Automation.PathInfo Name MemberType Definition —- ———- ———- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() Drive Property System.Management.Automation.PSDriveInfo Drive {get;} Path Property System.String Path {get;} Provider Property System.Management.Automation.ProviderInfo Provider {get;} ProviderPath Property System.String ProviderPath {get;} Print Page Previous Next Advertisements ”;

PowerShell – Environment Setup

Powershell – Environment Setup ”; Previous Next PowerShell Icon can be found in the task bar and in the start menu. Just by clicking on the icon, it will open. To open it, just click on the icon and then the following screen will open and it means that PowerShell is ready for you to work on. PowerShell Version The latest version of PowerShell is 5.0 and to check what is installed in our server we type the following command – :$PSVersionTable as shown in the following screenshot and from the screen we also know that we have PSVersion 4.0 To update with the latest version where it has more Cmdlets we have to download Windows Management Framework 5.0 from the following link − https://www.microsoft.com/en-us/download/details.aspx?id=50395 and install it. PowerShell ISE The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE, you can run commands and write, test, and debug scripts in a single Windows-based graphic user interface with multiline editing, tab completion, syntax coloring, selective execution, context-sensitive help, and support for right-to-left languages. You can use menu items and keyboard shortcuts to perform many of the same tasks that you would perform in the Windows PowerShell console. For example, when you debug a script in the Windows PowerShell ISE, to set a line breakpoint in a script, right-click the line of code, and then click Toggle Breakpoint. To open it you just go to Start – Search and then Type – PowerShell as shown in the following screenshot. Then click on Windows PowerShell ISE. Or click on the downward Arrow as shown in the following screenshot. It will list all the applications installed on the server and then click on Windows PowerShell ISE. The following table will be open − It has three sections, which include – The PowerShell Console with number 1, then Scripting File number 2 and the third is the Command Module where you can find the module. While creating the script you can run directly and see the result like the following example − PowerShell Basic Commands There are a lot of PowerShell commands and it is very difficult to put in all these commands in this tutorial, we will focus on some of the most important as well as basic commands of PowerShell. The first step is to go to the Get-Help command which gives you an explanation about how to give a command and its parameter. To get the list of Updates − Get-HotFix and to install a hot fix as follows Get-HotFix -id kb2741530 Print Page Previous Next Advertisements ”;

PowerShell – Files I/O

Powershell – File I/O Operations ”; Previous Next Following are the examples of powershell scripts of creating and reading different types of files. Sr.No. Operation & Description 1 Create Text File Example Script to show how to create a text file using PowerShell scripts. 2 Read Text File Example Script to show how to read a text file using PowerShell scripts. 3 Create XML File Example Script to show how to create a XML file using PowerShell scripts. 4 Read XML File Example Script to show how to read a XML file using PowerShell scripts. 5 Create CSV File Example Script to show how to create a CSV file using PowerShell scripts. 6 Read CSV File Example Script to show how to read a CSV file using PowerShell scripts. 7 Create HTML File Example Script to show how to create a HTML file using PowerShell scripts. 8 Read HTML File Example Script to show how to read a HTML file using PowerShell scripts. 9 Erasing file content Example Script to show how to erase file contents using PowerShell scripts. 10 Append Text Data Example Script to show how to append text to a file contents using PowerShell scripts. Print Page Previous Next Advertisements ”;

PowerShell – Advanced Cmdlets

Powershell – Advanced Cmdlets ”; Previous Next Cmdlets A cmdlet or “Command let” is a lightweight command used in the Windows PowerShell environment. The Windows PowerShell runtime invokes these cmdlets at command prompt. You can create and invoke them programmatically through Windows PowerShell APIs. Following are advanced usage example of cmdlets. Sr.No. Cmdlet Type & Description 1 Get-Unique Cmdlet Example program to showcase Get-Unique Cmdlet. 2 Group-Object Cmdlet Example program to showcase Group-Object Cmdlet. 3 Measure-Object Cmdlet Example program to showcase Measure-Object Cmdlet. 4 Compare-Object Cmdlet Example program to showcase Compare-Object Cmdlet. 5 Format-List Cmdlet Example program to showcase Format-List Cmdlet. 6 Format-Wide Cmdlet Example program to showcase Format-Wide Cmdlet. 7 Where-Object Cmdlet Example program to showcase Where-Object Cmdlet. 8 Get-ChildItem Cmdlet Example program to showcase Get-ChildItem Cmdlet. 9 ForEach-Object Cmdlet Example program to showcase ForEach-Object Cmdlet. 10 Start-Sleep Cmdlet Example program to showcase Start-Sleep Cmdlet. 11 Read-Host Cmdlet Example program to showcase Read-Host Cmdlet. 12 Select-Object Cmdlet Example program to showcase Select-Object Cmdlet. 13 Sort-Object Cmdlet Example program to showcase Sort-Object Cmdlet. 14 Write-Warning Cmdlet Example program to showcase Write-Warning Cmdlet. 15 Write-Host Cmdlet Example program to showcase Write-Host Cmdlet. 16 Invoke-Item Cmdlet Example program to showcase Invoke-Item Cmdlet. 17 Invoke-Expression Cmdlet Example program to showcase Invoke-Expression Cmdlet. 18 Measure-Command Cmdlet Example program to showcase Measure-Command Cmdlet. 19 Invoke-History Cmdlet Example program to showcase Invoke-History Cmdlet. 20 Add-History Cmdlet Example program to showcase Add-History Cmdlet. 21 Get-History Cmdlet Example program to showcase Get-History Cmdlet. 22 Get-Culture Cmdlet Example program to showcase Get-Culture Cmdlet. Print Page Previous Next Advertisements ”;