Powershell – Brackets ”; Previous Next Powershell supports three types of brackets. Parenthesis brackets. − () Braces brackets. − {} Square brackets. − [] Parenthesis brackets This type of brackets is used to pass arguments enclose multiple set of instructions resolve ambiguity create array Example > $array = @(“item1”, “item2”, “item3”) > foreach ($element in $array) { $element } item1 item2 item3 Braces brackets This type of brackets is used to enclose statements block commands Example $x = 10 if($x -le 20){ write-host(“This is if statement”) } This will produce the following result − Output This is if statement. Square brackets This type of brackets is used to access to array access to hashtables filter using regular expression Example > $array = @(“item1”, “item2”, “item3″) > for($i = 0; $i -lt $array.length; $i++){ $array[$i] } item1 item2 item3 >Get-Process [r-s]* Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName ——- —— —– —– —– —— — ———– 320 72 27300 33764 227 3.95 4028 SCNotification 2298 77 57792 48712 308 2884 SearchIndexer … Print Page Previous Next Advertisements ”;
Category: powershell
Powershell – Date and Time Operations ”; Previous Next Following are the examples of powershell scripts on System Date and Time. Sr.No. Operation & Description 1 Get System Date Example Script to show how to get system date using PowerShell scripts. 2 Set System Date Example Script to show how to set system date using PowerShell scripts. 3 Get System Time Example Script to show how to get system time using PowerShell scripts. 4 Set System Time Example Script to show how to set system time using PowerShell scripts. Print Page Previous Next Advertisements ”;
PowerShell – Looping
Powershell – Looping ”; Previous Next There may be a situation when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages − PowerShell programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail. Sr.No. Loop & Description 1 for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. 2 forEach loop Enhanced for loop. This is mainly used to traverse collection of elements including arrays. 3 while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. 4 do…while loop Like a while statement, except that it tests the condition at the end of the loop body. Print Page Previous Next Advertisements ”;
Powershell – Special Variables ”; Previous Next PowerShell Special variables store information about PowerShell. These are also called automatic variables. Following is the list of automatic variables − Operator Description $$ Represents the last token in the last line received by the session. $? Represents the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed. $^ Represents the first token in the last line received by the session. $_ Same as $PSItem. Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline. $ARGS Represents an array of the undeclared parameters and/or parameter values that are passed to a function, script, or script block. $CONSOLEFILENAME Represents the path of the console file (.psc1) that was most recently used in the session. $ERROR Represents an array of error objects that represent the most recent errors. $EVENT Represents a PSEventArgs object that represents the event that is being processed. $EVENTARGS Represents an object that represents the first event argument that derives from EventArgs of the event that is being processed. $EVENTSUBSCRIBER Represents a PSEventSubscriber object that represents the event subscriber of the event that is being processed. $EXECUTIONCONTEXT Represents an EngineIntrinsics object that represents the execution context of the PowerShell host. $FALSE Represents FALSE. You can use this variable to represent FALSE in commands and scripts instead of using the string “false”. $FOREACH Represents the enumerator (not the resulting values) of a ForEach loop. You can use the properties and methods of enumerators on the value of the $ForEach variable. $HOME Represents the full path of the user”s home directory. $HOST Represents an object that represents the current host application for PowerShell. $INPUT Represents an enumerator that enumerates all input that is passed to a function. $LASTEXITCODE Represents the exit code of the last Windows-based program that was run. $MATCHES The $Matches variable works with the -match and -notmatch operators. $MYINVOCATION $MyInvocation is populated only for scripts, function, and script blocks. PSScriptRoot and PSCommandPath properties of the $MyInvocation automatic variable contain information about the invoker or calling script, not the current script. $NESTEDPROMPTLEVEL Represents the current prompt level. $NULL $null is an automatic variable that contains a NULL or empty value. You can use this variable to represent an absent or undefined value in commands and scripts. $PID Represents the process identifier (PID) of the process that is hosting the current PowerShell session. $PROFILE Represents the full path of the PowerShell profile for the current user and the current host application. $PSCMDLET Represents an object that represents the cmdlet or advanced function that is being run. $PSCOMMANDPATH Represents the full path and file name of the script that is being run. $PSCULTURE Represents the name of the culture currently in use in the operating system. $PSDEBUGCONTEXT While debugging, this variable contains information about the debugging environment. Otherwise, it contains a NULL value. $PSHOME Represents the full path of the installation directory for PowerShell. $PSITEM Same as $_. Contains the current object in the pipeline object. $PSSCRIPTROOT Represents the directory from which a script is being run. $PSSENDERINFO Represents information about the user who started the PSSession, including the user identity and the time zone of the originating computer. $PSUICULTURE Represents the name of the user interface (UI) culture that is currently in use in the operating system. $PSVERSIONTABLE Represents a read-only hash table that displays details about the version of PowerShell that is running in the current session. $SENDER Represents the object that generated this event. $SHELLID Represents the identifier of the current shell. $STACKTRACE Represents a stack trace for the most recent error. $THIS In a script block that defines a script property or script method, the $This variable refers to the object that is being extended. $TRUE Represents TRUE. You can use this variable to represent TRUE in commands and scripts. Print Page Previous Next Advertisements ”;
Powershell – Files and Folder Operations ”; Previous Next Following are the examples of powershell scripts on Files and Folders. Sr.No. Operation & Description 1 Creating Folders Example Script to show how to create folder(s) using PowerShell scripts. 2 Creating Files Example Script to show how to create file(s) using PowerShell scripts. 3 Copying Folders Example Script to show how to copy file(s) using PowerShell scripts. 4 Copying Files Example Script to show how to create file(s) using PowerShell scripts. 5 Deleting Folders Example Script to show how to delete folder(s) using PowerShell scripts. 6 Deleting Files Example Script to show how to delete file(s) using PowerShell scripts. 7 Moving Folders Example Script to show how to move folder(s) using PowerShell scripts. 8 Moving Files Example Script to show how to move file(s) using PowerShell scripts. 9 Rename Folders Example Script to show how to rename folder(s) using PowerShell scripts. 10 Rename Files Example Script to show how to rename file(s) using PowerShell scripts. 11 Retrieving Item Example Script to show how to retrieve item(s) using PowerShell scripts. 12 Check Folder Existence Example Script to show how to check folder existence using PowerShell scripts. 13 Check File Existence Example Script to show how to check file existence using PowerShell scripts. Print Page Previous Next Advertisements ”;
PowerShell – Home
Powershell Tutorial Quick Guide Resources Job Search Discussion 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. Audience This tutorial has been designed for all those readers who want to learn the features of Windows Server 2012. It is especially going to be useful for all those professionals who are required to install and use this operating system to perform various duties in their respective organizations. Prerequisites We assume the readers of this tutorial have a practical experience of handling a Windows based Servers. In addition, it is going to help if the readers have a basic knowledge of how to install and use an operating system. Print Page Previous Next Advertisements ”;
PowerShell – Hashtables
Powershell – Hashtables ”; Previous Next Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. Generally we used String or numbers as keys. This tutorial introduces how to declare hashtable variables, create hashtables, and process hashtable using its methods. Declaring hashtable Variables To use an hashtable in a program, you must declare a variable to reference the hashtable. Here is the syntax for declaring an hashtable variable − Syntax $hash = @{ ID = 1; Shape = “Square”; Color = “Blue”} or $hash = @{} Note − Ordered dictionaries can be created using similar syntax. Ordered dictionaries maintain the order in which entries are added whereas hashtables do not. Example The following code snippets are examples of this syntax − $hash = [ordered]@{ ID = 1; Shape = “Square”; Color = “Blue”} Print the hashtable. $hash Output Name Value —- —– ID 1 Color Blue Shape Square The hashtable values are accessed through the keys. > $hash[“ID”] 1 Processing Hashtable Dot notation can be used to access hashtables keys or values. > $hash.keys ID Color Shape > $hash.values 1 Blue Square Example Here is a complete example showing how to create, initialize, and process hashtable − $hash = @{ ID = 1; Shape = “Square”; Color = “Blue”} write-host(“Print all hashtable keys”) $hash.keys write-host(“Print all hashtable values”) $hash.values write-host(“Get ID”) $hash[“ID”] write-host(“Get Shape”) $hash.Number write-host(“print Size”) $hash.Count write-host(“Add key-value”) $hash[“Updated”] = “Now” write-host(“Add key-value”) $hash.Add(“Created”,”Now”) write-host(“print Size”) $hash.Count write-host(“Remove key-value”) $hash.Remove(“Updated”) write-host(“print Size”) $hash.Count write-host(“sort by key”) $hash.GetEnumerator() | Sort-Object -Property key This will produce the following result − Output Print all hashtable keys ID Color Shape Print all hashtable values 1 Blue Square Get ID 1 Get Shape print Size 3 Add key-value Add key-value print Size 5 Remove key-value print Size 4 sort by key Name Value —- —– Color Blue Created Now ID 1 Shape Square Print Page Previous Next Advertisements ”;
PowerShell – Backtick
Powershell – Backtick ”; Previous Next Backtick (`) operator is also called word-wrap operator. It allows a command to be written in multiple lines. It can be used for new line (`n) or tab (`t) in sentences as well. See the examples below − Example 1 Get-Service * | Sort-Object ServiceType ` | Format-Table Name, ServiceType, Status -AutoSize It will become Get-Service * | Sort-Object ServiceType | Format-Table Name, ServiceType, Status -AutoSize Verify the output as Name ServiceType Status —- ———– —— MSSQLServerADHelper100 Win32OwnProcess Stopped ntrtscan Win32OwnProcess Running … Example 2 Use of new line and tab. > Write-host “Title Subtitle” Title Subtitle > Write-host “Title `nSubtitle” Title Subtitle > Write-host “Title `tSubtitle” Title Subtitle Print
PowerShell – Alias
Powershell – Alias ”; Previous Next PowerShell alias is another name for the cmdlet or for any command element. Creating Alias Use New-Alias cmdlet to create a alias. In the below example, we”ve created an alias help for Get-Help cmdlet. New-Alias -Name help -Value Get-Help Now invoke the alias. help Get-WmiObject -Detailed You will see the following output. NAME Get-WmiObject SYNOPSIS Gets instances of Windows Management Instrumentation (WMI) classes or information about the available classes. SYNTAX Get-WmiObject [ … Getting Alias Use get-alias cmdlet to get all the alias present in current session of powershell. Get-Alias You will see the following output. CommandType Name Definition ———– —- ———- Alias % ForEach-Object Alias ? Where-Object Alias ac Add-Content Alias asnp Add-PSSnapIn … Print Page Previous Next Advertisements ”;
PowerShell – Quick Guide
Powershell – Quick Guide ”; Previous Next Powershell – Overview Windows PowerShell is a command-line shell and scripting language designed especially for system administration. It”s 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. 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. 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. Powershell – Environment Setup 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 Powershell – 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. Cmdlet vs Command Cmdlets are way different from commands in other command-shell environments in the following manners − Cmdlets are .NET Framework class objects; and not just stand-alone executables. Cmdlets can be easily constructed from as few as a dozen lines of code. Parsing, error presentation, and output formatting are not handled by cmdlets. It is done by the Windows PowerShell runtime. Cmdlets process works on objects not on text stream and objects can be passed as output for pipelining. Cmdlets are record-based as they process a single object at a time. Getting Help 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. Powershell – Files and Folder Operations Following are the examples of powershell scripts on Files and Folders. Sr.No. Operation & Description 1 Creating Folders Example Script to show how to create folder(s) using PowerShell scripts. 2 Creating Files Example Script to show how to create file(s) using PowerShell scripts. 3 Copying Folders Example Script to show how to copy file(s) using PowerShell scripts. 4 Copying Files Example Script to show how to create file(s) using PowerShell scripts. 5 Deleting Folders Example Script to show how to delete folder(s) using PowerShell scripts. 6 Deleting Files Example Script to show how to delete file(s) using PowerShell scripts. 7 Moving Folders Example Script