Scripting >> Powershell >> Operators >>  What is the syntax for -f format operator


get-service -
windows workflow foundation
require .net framework + wmf
w2008 v1.0 ... v3.0
v4.0 supported OS
- w7

workflows can survive reboot

dsc desired state configuration
- can log deviations
- can autocorrect

AltenateNames - aliases
- built in aliases
- user defined aliases

external commands run as separate process
- cannot discover the syntax

cmdlets
- native ps commands
- does not launch in separate process
- single purpose
- verb-noun naming
- noun in singular
- now Verb-PrefixNoun
  eg. prefix VM, AD

verb-noun -parameter value -switch

get-command cmdlet -syntax
e.g.
get-command test-connection -syntax
get-command -verb verbpattern
get-command -noun nounpattern
get-command -parameter

show-command is the gui version of get-command
get-help is the detailed version and includes examples
eg..
get-help get-service -examples
get-help get-service -showwindow  ... gui version

general help on concept
get-help about_*

tabs in the help = parameter sets

-outvariable <variablenamewithout$> captures cmdlet output into
variable
or
$var = cmdlet

common parameters
ErrorAction
ErrorVariable
OutVariable
etc

command termination
; or newline

get-content filename to read from file

get-process | get-member
will show the properties and methods of process

how to get top 10 process by virtual memory usage
get-process | sort-object -property VM | select-object -first 10 `
   -Property ProcessName,VirtualMemorySize

get-process | sort-object -property VM | select-object -first 10 `
   -Property ProcessName,VirtualMemorySize | export-csv -path c:\temp\a.csv

invoke-item -path c:\temp\file.csv  => same as double click on the file

$_ holds the each item in a foreach loop
or
$PSitem

to select a particular property of each object, use $_.<property> e.g. $_.Name

split-path splits parent\leaf into -Parent & -Leaf

compare-object -referenceobject firstobject -differenceobject secondobject

use format-* at the pipeline output stage

to output to file | out-file -filepath

exercise
- get all dlls in c:\windows\assembly and in all subfolders
- 10 largest by size

Get-ChildItem *.dll -path C:\windows\assembly -Recurse |
Sort-Object -Property Length -Descending |
Select-Object -First 10 -Property Name,Length |
export-csv -path c:\scripts\a3.csv -NoTypeInformation

invoke-item c:\scripts\a3.csv

or

Get-ChildItem -path C:\windows\assembly\* -Include *.dll -Recurse |
Sort-Object -Property Length -Descending |
Select-Object -First 10 -Property Name,Length |
export-csv -path c:\scripts\a3.csv -NoTypeInformation

invoke-item c:\scripts\a3.csv

how to introduce custom property using hash table
@{name="columnname",e={$_.Actualproperty * some calculation}}

to output 2 decimal places
"{0:N2}" -f somearray

To find commands that accept ParameterName ScriptBlock

get-command -ParameterName ScriptBlock

to measure execution time use measure-command -expression {}

function Get-Food
{
   param
   (
      $food = "rice",
      $drink = "water"
   )

   "I want $food and $drink"
}

' for literal i.e. no variable expansion of $var
" for  i.e. expands $var

remoting
(1) DCOM-RPC  (legacy, port 135 & then random) e.g. cmdlets that use ComputerName parameter
(2) WS-MAN (port 5985 http, 5986 https) e.g. invoke-command & enter-pssesssion

powershell.exe or ise.exe on the source host
wsmprovhost.exe is the process on the remote machine

to allow remoting, do on the server

> enable-psremoting

ensure firewall ports opened