My personal PowerShell ISE snippets

Following the post of Josh RICKARD on 4SysOps about ISE snippets,
I just wanted to share two of my personal snippets I use very often as a System Administrator.

Foreach function with a server list from an array

$SnippetText = @'
$ServerList=@(
'Server1'
'Server2'
)
foreach($Server in $ServerList)
{

}
'@

$NewSnippetParams = @{
Title       = 'Foreach with server list from array'
Description = 'Foreach function using an array of servers'
Text        = $SnippetText
CaretOffset = 17
Author      = 'Luc FULLENWARTH'
}

New-IseSnippet @NewSnippetParams

Foreach function with a server list imported from an file

$SnippetText = @'
$ServerList = Get-Content -Path C:\Temp\MyList.txt
foreach ($Server in $ServerList)
{

}
'@

$NewSnippetParams = @{
Title       = 'Foreach with server list from file'
Description = 'Foreach function using a file to get the server list'
Text        = $SnippetText
CaretOffset = 33
Author      = 'Luc FULLENWARTH'
}

New-IseSnippet @NewSnippetParams

Help section

$SnippetText = @'
<#
.SYNOPSIS

Adds a file name extension to a supplied name.

.DESCRIPTION

Adds a file name extension to a supplied name.
Takes any strings for the file name or extension.

.PARAMETER Name
Specifies the file name.

.PARAMETER Extension
Specifies the extension. "Txt" is the default.

.INPUTS

None. You cannot pipe objects to Add-Extension.

.OUTPUTS

System.String. Add-Extension returns a string with the extension
or file name.

.NOTES

Additional information

.EXAMPLE

C:\PS> extension -name "File"
File.txt

.EXAMPLE

C:\PS> extension -name "File" -extension "doc"
File.doc

.EXAMPLE

C:\PS> extension "File" "doc"
File.doc

.LINK

http://www.fabrikam.com/extension.html

.LINK

Set-Item
#>
'@

$NewSnippetParams = @{
Title       = 'Help section'
Description = 'Creates a Help section with the most common sections'
Text        = $SnippetText
CaretOffset = 14
Author      = 'Luc FULLENWARTH'
}

New-IseSnippet @NewSnippetParams

Send-MailMessage

I shared this one on the VSCode PowerShell Extension community snippet list

$SnippetText = @'
$Params = @{
'SmtpServer'  = 'smtp.mycompany.com'
'Port'        =  25
'Priority'    = 'Normal'
'From'        = 'sender@mycompany.com'
'To'          = 'mainrecipient@mycompany.com'
'Cc'          = 'copyrecipient@mycompany.com'
'Bcc'         = 'hiddenrecipient@mycompany.com'
'Subject'     = 'Mail title'
'Body'        = 'This is the content of my mail'
'BodyAsHtml'  =  $false
'Attachments' = 'c:\\MyFile.txt'
}
Send-MailMessage @Params
'@

$NewSnippetParams = @{
Title       = 'Send-MailMessage'
Description = 'Send-MailMessage cmdlet with splatting for parameters'
Text        = $SnippetText
CaretOffset = 35
Author      = 'Luc FULLENWARTH'
}

New-IseSnippet @NewSnippetParams

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s