Showing posts with label Various. Show all posts
Showing posts with label Various. Show all posts

November 20, 2012

How to create a new Snippet in PowerShell ISE 3

Paste this code in a PowerShell ISE new tab :

[scriptblock]$code = 
{#PutYourCodeHere
}

New-IseSnippet `
    -Text $code.ToString() `
    -CaretOffset $code.ToString().Length `
    -Force `
    -Title ‘SnippetTitle’ `
    -Description ‘SnippetDescription’ 

Replace the #PutYourCodeHere by your Powershell commands, set the snippet Title, description and run it. Why not make a snippet to create snippet.

References :

August 29, 2012

How to display a number in hexdecimal format

To display 32255 in hexadecimal format

"{0:X}" -f 32255

Note :

To use the hexadecimal number (32225) in expressions, type : 0x7dff

Ref :

April 7, 2010

How to launch a script from the command line ?

powershell.exe -nologo -f Send-UserExpirationMail.ps1

How to quit a script

Exit

This is equivalent to wscript.quit in VBScript.

How to send an email ?

$message = '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><html><head></head><body lang=FR-CA>This is the body of the message</body></html>"
send-MailMessage -to "toto@domain.com" -Encoding ([System.Text.Encoding]::UTF8) -subject "PowerShell Rocs" -from "bozo@domain.com"  -bodyasHTML -body $message -smtpserver "mysmtp.domain.com"

If you want to send your mail to multiple recipients, use the following syntax for the -to  parameter :

@("bozo@domain.com","toto@domain.com")

How to search for a command ?

Get-command *content

How to read and display a text file ?

Get-content c:\boot.ini | foreach-object {$_}

How to use the «If» command ?

if (1 -eq 2) {echo "True"} else {echo "False"}

You can use any of the comparison operators in your «If» command :

-eq Equal
-ne Not equal
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal to
-like Wild card comparison : “powershell” –like “pow*”
-notlike Negative wild card comparison
-match Regular expression comparison
-notmatch Negative Regular expression comparison
-contains “powershell” –contains “Shell”
-notcontains “powershell” –notcontains “Shell”

All those comparison operators are case-insensitive.

You ca also use any of the logical operators :

-and
-or
-xor
-not
!

What is the difference between single (‘) and double quotes (“) ?

With double quotes (“), variables in the string are expanded.

write-output "Starting engine from $pshome ..."
Will output :
Starting engine from C:\Windows\System32\WindowsPowerShell\v1.0 ...
To expand an expression use $(), ex:
Write-Output "the Powershell host is $($host.name)"