Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Powershell Intro/Basics

1

Category :

Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of .NET Framework.

Perform administrative tasks on both local and remote Windows systems.

PowerShell output is always a .NET object, basically it could be any .NET object whose assembly is loaded into PowerShell including your own .NET objects.

Discoverability

Windows PowerShell makes it easy to discover its features. For example, to find a list of cmdlets that view and change Windows services, type:
Get-Command *-Service
After discovering which cmdlet accomplishes a task, you can learn more about the cmdlet by using the Get-Help cmdlet. For example, to display help about the Get-Service cmdlet, type:
Get-Help Get-Service
Most cmdlets return objects which can be manipulated and then rendered into text for display. To fully understand the output of that cmdlet, pipe its output to the Get-Member cmdlet. For example, the following command displays information about the members of the object output by the Get-Service cmdlet.
Get-Service | Get-Member

Online Help You can get online help for any command using the
Get-Help {command-name} -Online
Get-Help | Get-Service -Online

PowerShell syntax

semi colons

Not needed to terminate statements. You can however use them to separate statements on the command line.

escape character

The backtick (grave accent) represented as the character ` is the escape character within PowerShell. You can use this character to for example print tabs `t or escape characters with a special meaning like the $ character as in `$ or escaping quotes like `”.
You can also use the backtick character to span your statements over multiple lines, which can sometimes come in handy.

variables

Variables in PowerShell have a ‘$’ sign in front of them, like
$myvar = 'some value'
$myothervar = 42

single & double quotes

There actually is a distinction when using these. The single quote represents everything literally, the double quotes interprets what’s inside it.
$dummyvalue = 'dummy'
write 'test$dummyvalue'
write "test$dummyvalue"
The first one will print ‘test$dummyvalue’ the second one will print ‘testdummy’. And like most scripting language you can use the quotes to encapsulate each other like “soo he said ‘wow’, jus he did”.

brackets and colons/periods

The round brackets, parenthesis, ‘(‘ and ‘)’ can be used to specify order and to pass arguments to .NET methods. When calling function you defined in PowerShell you pass the arguments SPACE separated and without parenthesis since they could cause an error.

The square brackets can be used to access list/array members like most languages: The below will produce the output ‘1’ since it’s accessing the first members in the array.
$test = 1,2,3,4,5
write $test[0]
The other use case for these brackets is to define types and accessing .NET classes as in: will print the current date time. Like you can see here you can use a double colon to access properties of a class,
$test = [DateTime]
write $test::now
to access methods you can use the period character
$test = New-Object System.datetime
write $test
write $test.AddYears(2015)

Functions

So there is an important distinctions between calling functions and calling methods. Functions in PowerShell use spaces for the parameters and not brackets, which you might confuse in the beginning when you mix in calls to methods.

CmdLet v Script v Function

Script

A file containing a collection of commands which are executed. A script can contain functions.

Function

Is also a collection of commands which are executed but it must be present in the session to be executed.
Function Say-Hello {
  <#
    .Synopsis
      A brief summary of the function
    .Description
      A full description of the function
    .Parameter Name
      The name parameter
  #>

  Param(
    [String]$Name = "everyone"
  )

  Return "Hello $Name"
}

Cmdlet

A cmdlet is .NET code which performs a specific task without you having to write it out yourself.

my thanks to:

some links to good resources below:
https://diablohorn.com/2016/02/06/powershell-overview-and-introduction/

blog series referenced in previous link above:
https://www.darkoperator.com/powershellbasics/

https://docs.microsoft.com/en-us/powershell/scripting/powershell-scripting?view=powershell-6