|
Guy recommends :
Find out which of your VMs are a waste of space and which VMs need more resources.
|
Windows PowerShell's SyntaxIntroduction to PowerShell's SyntaxThe fact that you almost don't need this page is a testament to the intuitive nature of PowerShell. Yet for those who wish to save time fumbling with the PowerShell syntax, it may pay to have a refresher of these rules of scripting grammar. Windows PowerShell Syntax Topics
♣ Case InsensitivePowerShell is fundamentally case insensitive. Every object and every cmdlet is case insensitive. Set-Location performs exactly the same action as set-location. However, where your data has case sensitive values, there are PowerShell operators to deal with 'case'. For example, -gt means greater than, -match means contains a particular string value. Now you can force these and similar operators to be case sensitive by prefixing with a 'c'. -cmatch, or -cgt mean that the comparison will be case sensitive. Comma and Semi-colonFor many years a bad attitude to syntax hindered me. My breakthrough was realizing that punctuation marks are there to aid the readers' understanding; my mistake was thinking syntax rules were designed by my English teacher as a way of finding new ways to tell me off. With PowerShell's syntax the comma is frequently used to separate items on a list. Whereas the semi-colon is to split separate ideas. Let us study this example: Clear-host Note 1: Each $Item is separated by a comma. No sign of the semi-colon, yet. Note 2: The comma is also used so separate items in an array {0,-30} Suppose we want to count the number of eventlogs. Let us introduce a variable $i Clear-host Note 1: The counter variable, $i++ is new element, which is not connected to the list; time for a semi-colon before the counter variable. = Equals and ! Not equalThe equals sign (=) behaves just as expected. As usual, '=' tests for equivalence, or sets a variable to be equal to a certain value. The equals sign has a counterpart ! (Exclamation mark) meaning, 'not equal'. You may also employ -not instead of ! I just include these two basic operators, '=' and ! for completeness. Hyphen -dash -minusSome people call this symbol (-) minus, others a refer to this sign as a dash, I mostly call it a hyphen. Let me be clear, this character maps to ASCII 45, to see the character, hold down ALT key, type 45 on numeric keypad, now let go of ALT key. PowerShell uses this - symbol for two purposes. Firstly, to join verb-Noun pairs, for example out-File guy.txt. Secondly, this minus sign is also used for parameters, modifiers, or filters such as -list; as in Get-Eventlog -list. The trap I fall into is to put a space between the minus and the modifier. get -eventlog is clearly wrong, because there is a space between get and -. The correct format is, Get-Eventlog, with no space.
Guy
Recommends: WMI Monitor and It's Free!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operator |
Definition of PowerShell Syntax |
| # | # The hash key is for comments |
| + | Add |
| - | Subtract |
| * | Multiply |
| / | Divide |
| % | Modulus (Some call it Modulo) - Means remainder 17 % 5 = 2 Remainder |
| = | equal |
| -not | logical not equal |
| ! | logical not equal |
| -band | binary and |
| -bor | binary or |
| -bnot | binary not |
| -replace | Replace (e.g. "abcde" -replace "b","B") (case insensitive) |
| -ireplace | Case-insensitive replace (e.g. "abcde" -ireplace "B","3") |
| -creplace | Case-sensitive replace (e.g. "abcde" -creplace "B","3") |
| -and | AND (e.g. ($a -ge 5 -AND $a -le 15) ) |
| -or | OR (e.g. ($a -eq "A" -OR $a -eq "B") ) |
| -is | IS type (e.g. $a -is [int] ) |
| -isnot | IS not type (e.g. $a -isnot [int] ) |
| -as | convert to type (e.g. 1 -as [string] treats 1 as a string ) |
| .. | Range operator (e.g. foreach ($i in 1..10) {$i } ) |
| & | call operator (e.g. $a = "Get-ChildItem" &$a executes Get-ChildItem) |
| . (dot followed by space) | call operator (e.g. $a = "Get-ChildItem" . $a executes Get-ChildItem in the current scope) |
| . |
.Period or .full stop for an objects properties $CompSys.TotalPhysicalMemory |
| -F | Format operator (e.g. foreach ($p in Get-Process) { "{0,-15} has {1,6} handles" -F $p.processname,$p.Handlecount } ) |

Import users from a spreadsheet. Just provide a list of the users with their fields in the top row, and save as .csv file. Then launch this FREE utility and match your fields with AD's attributes, click to import the users. Optionally, you can provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
Download your FREE bulk import tool.
| Operator |
Definition of PowerShell Syntax |
| -lt | Less than |
| -le | Less than or equal to |
| -gt | Greater than |
| -ge | Greater than or equal to |
| -eq | Equal to |
| -ne | Not Equal to |
| -contains |
Determine elements in a group. This always returns Boolean $True or $False. |
| -notcontains |
Determine excluded elements in a group This always returns Boolean $True or $False. |
| -like | Like - uses wildcards for pattern matching |
| -notlike | Not Like - uses wildcards for pattern matching |
| -match | Match - uses regular expressions for pattern matching |
| -notmatch | Not Match - uses regular expressions for pattern matching |
| Bitwise | |
| -band | Bitwise AND |
| -bor | Bitwise OR |
| -is | Is of Type |
| -isnot | Is not of Type |
| Other PowerShell Operators | |
| if(condition) | If condition (See more on PowerShell's If) |
| elseIf(condition) | ElseIF |
| else(condition) | Else |
| > |
Redirect, for example, output to text file Example .\cmdlet > stuff.txt |
| >> | Same as Redirect except it appends to an existing file |
Every language must have its grammar rules. However, with PowerShell syntax the rules for brackets, quotation marks and commas, all seem logical, straightforward and above all, consistent.
If you like this page then please share it with your friends
• PowerShell Tutorials • Syntax • Pipeline • Quotes • New-Item • Remove-Item • ItemProperty
• Select-String • -replace string • Group-Object • Sort-Object
• Windows PowerShell cmdlets • Windows PowerShell
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.

You get 36 topics organized into these 3 sections:
1) Getting Started
2) Real-life tasks
3) Examples of Syntax.
In addition to the ebook, you get a PDF version of this Introduction to PowerShell ebook It runs to 120 pages of A4.
*
Custom Search
|
Guy Recommends: WMI Monitor and It's Free!
|
|
Home Copyright © 1999-2012 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |