<<

Automate and manage your environment using PowerShell Core 6.0

©

www.packtpub.com

mapt.io

www.packt.com [email protected]

www.packt.com

pauby.com@pauby

authors.packtpub.com

Chapter 1

Chapter 2

Chapter 3

Chapter 4

Chapter 5

Chapter 6foreachfor whiledo

Chapter 7 using

Chapter 8

Chapter 9

Chapter 10

Chapter 11

Chapter 12

Chapter 13

Chapter 14

Chapter 15

Chapter 16

Chapter 17param

Chapter 18

Chapter 19

Chapter 20

Chapter 21 trycatchfinallytrap

www.packt.com www.packt.com/support

www.packt.com

https://github.com/ PacktPublishing/Mastering-Windows-PowerShell-Scripting-Third-Edition

https://github.com/PacktPublishing/

https://www.packtpub.com/sites/default/files/ downloads/9781789536669_ColorImages.pdf

CodeInText Get-

Get- -CommandType Cmdlet, Function | Where-Object { $metadata = New-Object System.Management.Automation.CommandMetadata($_) $metadata.ConfirmImpact -eq 'High' }

{ $metadata = New-Object System.Management.Automation.CommandMetadata($_) $metadata.ConfirmImpact -eq 'High' }

[email protected]

www.packt.com/submit-errata

[email protected]

authors.packtpub.com

packt.com



Chapter 1 Chapter 2 Chapter 3 Chapter 4 

http://code.visualstudio.com

https://marketplace.visualstudio.com/VSCode

Get-Help Save-Help Update-Help

Get-Module -ListAvailable | Where- Object HelpInfoURI -like *

Get-Help

Save-Help Out-Null

NAME Out-Null SYNTAX Out-Null [-InputObject ] []

ALIASES None

REMARKS Get-Help cannot the Help files for this cmdlet on this computer. It is displaying only partial help. -- To download and install Help files for the module that includes this cmdlet, use Update-Help. -- To view the Help topic for this cmdlet online, : "Get-Help Out-Null -Online" or go to http://go.microsoft.com/fwlink/LinkID=113366.

help

Get-Help helpGet-Help default

TOPIC Windows PowerShell Help System

SHORT DESCRIPTION Displays help about Windows PowerShell cmdlets and concepts.

LONG DESCRIPTION Windows PowerShell Help describes Windows PowerShell cmdlets,

help Get-Help help default

Get-Help

Get-Help

Get-Help

SYNTAX Get-Process [[-Name] ] [-ComputerName ] [-FileVersionInfo] [-Module] []

Get-Process [-ComputerName []] [-FileVersionInfo] [-Module] -InputObject []

Get-Process

SYNTAX Get-Process [[-Name] ] [-ComputerName ] [- FileVersionInfo] [-Module] []

Get-Process Get-Process

Get-Command Get-Command Get-Variable -Syntax

Get-Help

PathImport-Csv

-Path [] Specifies the path to the CSV file to import. You can also pipe a path to Import-Csv. Required false Position 1 Default value None Accept pipeline input true (ByValue) Accept wildcard characters false

DetailedGet-Help

Detailed

FullDetailed DetailedGet-Process

– ... INPUTS System.Diagnostics.Process You can pipe a process object to Get-Process.

OUTPUTS System.Diagnostics.Process, System.Diagnotics.FileVersionInfo, System.Diagnostics.ProcessModule By default, Get-Process returns a System.Diagnostics.Process object. If you use the FileVersionInfo parameter, it returns a System.Diagnotics.FileVersionInfo object. If you use the Module parameter (without the FileVersionInfo parameter), it returns a

Save-Help DnsClient C:\PSHelp

HelpInfo

CABhelp

Update- Help

Update-Help

help help

Update-Help

$PSHost (%SystemRoot%\System32\WindowsPowerShell\v1.0)

Update-Help 24 hours helpVerbose

VERBOSE: Help was not updated for the module DnsClient, because the Update- Help command was run on this computer within the last 24 hours. To update help again, add the Force parameter to your command.

Force SourcePath

Update-Help : Failed to update Help for the module(s) 'DnsClient' with UIculture(s) {en-GB} : Unable to retrieve the HelpInfo XML file for UI culture en-GB. Make sure the HelpInfoUri property in the module manifest is valid or check your network connection and then try the command again. line:1 char:1 + Update-Help -SourcePath C:\Temp -Module DnsClient -Verbose -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [Update-Help], Exception + FullyQualifiedErrorId : UnableToRetrieveHelpInfoXml,Microsoft.PowerShell.Commands.UpdateHelpCommand

en-GBGet-UICulture en-US

UICultureUpdate- Help

About

Get-HelpHelpFile

Name Category Synopsis ------about_Aliases HelpFile SHORT DESCRIPTION about_Arithmetic_Operators HelpFile SHORT DESCRIPTION about_Arrays HelpFile SHORT DESCRIPTION about_Assignment_Operators HelpFile SHORT DESCRIPTION about_Automatic_Variables HelpFile SHORT DESCRIPTION about_Break HelpFile SHORT DESCRIPTION about_Classes HelpFile SHORT DESCRIPTION about_Command_Precedence HelpFile SHORT DESCRIPTION about_Command_Syntax HelpFile SHORT DESCRIPTION about_Comment_Based_Help HelpFile SHORT DESCRIPTION about_CommonParameters HelpFile SHORT DESCRIPTION about_Comparison_Operators HelpFile SHORT DESCRIPTION about_Continue HelpFile SHORT DESCRIPTION about_Core_Commands HelpFile SHORT DESCRIPTION about_Data_Sections HelpFile SHORT DESCRIPTION ...

AddGetSetNew ConvertFromConvertTo

ProtectUnprotect

https://docs.microsoft.com/en-gb/powershell/developer/ cmdlet/approved-verbs-for-windows--commands

Get-ProcessNew-ItemGet- HelpGet-ChildItemInvoke-WebRequest Send-MailMessage

NetSecurity Get

CommandType Name Version Source ------Function Get-NetFirewallAddressFilter 2.0.0.0 NetSecurity Function Get-NetFirewallApplicationFilter 2.0.0.0 NetSecurity Function Get-NetFirewallInterfaceFilter 2.0.0.0 NetSecurity Function Get-NetFirewallInterfaceTypeFilter 2.0.0.0 NetSecurity Function Get-NetFirewallPortFilter 2.0.0.0 NetSecurity Function Get-NetFirewallProfile 2.0.0.0 NetSecurity Function Get-NetFirewallRule 2.0.0.0 NetSecurity Function Get-NetFirewallSecurityFilter 2.0.0.0 NetSecurity Function Get-NetFirewallServiceFilter 2.0.0.0 NetSecurity Function Get-NetFirewallSetting 2.0.0.0 NetSecurity

Get-NetFirewallRule

Get-Help

Chapter 2

Get-Alias

CommandType Name ------Alias % -> ForEach-Object Alias  -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias cat -> Get-Content Alias -> Set-Location

Get-Alias

%ForEach-Object Where-Object cdSet-Location gccatGet-Content lsGet-ChildItem manhelpGet-Help

New-Alias grepSelect-String

Get-Help about_Aliases

Get-Help

SYNTAX Get-Process [-ComputerName ]

SYNTAX Get-Process [[-Name] ] ...

SYNTAX Get-ADUser -Filter ...

Filter FilterFilter

SYNTAX Get-ADUser [-Identity] ...

Identity

Identity

SYNTAX Add-Member [-NotePropertyName] [-NotePropertyValue] ...

RecurseGet-ChildItem

SYNTAX Get-ChildItem ... [-Recurse] ...

falsetrue false

Get-ChildItem Confirm

CommonParameters

SYNTAX Get-Process ... []

Stop-ProcessVerbose Verbosenotepad

VERBOSE: Performing the operation "Stop-Process" on target "notepad (5592)".

Get-ProcessVerbose

Get-CimInstanceClassName

SYNTAX Get-CimInstance [-ClassName] ...

Get-ProcessName

SYNTAX Get-Process [[-Name] ] ...

Get-Process

Stop-Process

SYNTAX Stop-Process [-Id] [-Confirm] [-Force] [-PassThru] [-WhatIf] [] Stop-Process [-InputObject] [-Confirm] [-Force] [-PassThru] [-WhatIf] [] Stop-Process [-Confirm] [-Force] -Name [-PassThru] [-WhatIf] []

IdInputObject Name

InputObject

Get-Help

Get-HelpGet-Command (Get-Command Stop- Process).Parameters.InputObject.Attributes

ConfirmWhatIfForce SetRemove

ConfirmWhatIfPreference about

Force

ForceForce HelpForce Remove-Item

Confirm ConfirmRemove-Item

Confirm Are you sure you want to perform this action Performing the operation "Remove File" on target "C:\Users\\AppData\Local\Temp\IMadeThisUp.txt". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [] Help (default is "Y"):

Confirmfalse

Confirm ConfirmPreference Remove-Item Recurse

Clear- RecycleBin

Confirm Are you sure you want to perform this action Performing the operation "Clear-RecycleBin" on target " All of the contents of the Recycle Bin". [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [] Help (default is "Y"):

ConfirmfalseClear-RecycleBin

Get-Command -CommandType Cmdlet, Function | Where-Object { $metadata = New-Object System.Management.Automation.CommandMetadata($_) $metadata.ConfirmImpact -eq 'High' }

Confirm ConfirmPreference