├── Chapter01 ├── 4.2.1_Get-Help.ps1 ├── 5.1.1_Get-Verb.ps1 ├── 5.4.1_Get-Alias.ps1 ├── 4.4.1_Update-Help.ps1 ├── 7.2.1_Get-PSDrive.ps1 ├── 4.1.1_Get-Help.ps1 ├── 7.1.2_Get-PSProvider.ps1 ├── 5.4.2_Get-Alias_ByName.ps1 ├── 7.1.1_Providers.ps1 ├── 4.2.2_Get-Help_more.ps1 ├── 4.2.5_Get-Help_Example.ps1 ├── 4.1.2_UpdatableHelp.ps1 ├── 4.2.11_Get-Help_Full.ps1 ├── 4.2.6_Get-Help_Online.ps1 ├── 4.5.1_Get-Help_About.ps1 ├── 6.1.5_SwitchParam.ps1 ├── 6.1.6_CommonParams.ps1 ├── 6.4.11_WhatIfPreference.ps1 ├── 6.4.2_Force.ps1 ├── 6.4.5_Clear-RecycleBin_Confirm.ps1 ├── 4.2.10_Get-Help_Detailed.ps1 ├── 4.2.3_Get-Help_ShowWindow.ps1 ├── 4.2.8_Get-Help_Examples.ps1 ├── 5.3.2_Get-Command_Wildcards.ps1 ├── 5.5.3_AliasUse.ps1 ├── 5.5.4_New-Alias.ps1 ├── 6.4.8_ConfirmPreference_Set.ps1 ├── 7.1.4_ProviderHelpCategory.ps1 ├── 7.2.4_Get-ChildItem_HKU.ps1 ├── 4.2.4_Get-Help_All.ps1 ├── 4.2.9_Get-Help_Parameter.ps1 ├── 4.3.2_Save-Help_All.ps1 ├── 4.4.3_Update-Help_ImportHelp.ps1 ├── 8.4.1_ExampleCommand.ps1 ├── 5.3.1_Get-Command.ps1 ├── 5.5.2_Get-Alias_ByDefinition.ps1 ├── 6.3.1_ParameterSets.ps1 ├── 6.4.1_PreferenceVariables.ps1 ├── 6.4.4_Confirm_False.ps1 ├── 4.4.2_Update-Help_SingleModule.ps1 ├── 6.2.1_ParameterValues.ps1 ├── 6.4.6_Clear-RecycleBin_Confirm_False.ps1 ├── 7.1.3_ProviderHelp.ps1 ├── 4.3.1_Save-Help.ps1 ├── 6.2.2_ParameterValues_Array.ps1 ├── 7.2.2_CertProvider.ps1 ├── 7.3.1_UsingProviders_FileSystem.ps1 ├── 6.1.1_PositionalParams.ps1 ├── 6.1.2_MandatoryNamedParams.ps1 ├── 6.1.7_Verbose.ps1 ├── 6.4.7_ConfirmPreference.ps1 ├── 7.2.3_New-PSDrive.ps1 ├── 6.4.10_BypassingWhatIf.ps1 ├── 7.4.1_UsingProviders_Certificate.ps1 ├── 6.1.3_MandatoryPositionalParams.ps1 ├── 6.4.12_ConfirmAndWhatIfPreference.ps1 ├── 8.1.1_Splatting.ps1 ├── 4.2.7_Get-Help_Get-Process.ps1 ├── 8.4.2_PositionalSplatting.ps1 ├── 4.4.5_Update-Help_NamedCulture.ps1 ├── 8.1.2_Splatting_CallOperator.ps1 ├── 8.1.3_Splatting_CallOperator.ps1 ├── 4.4.4_Update-Help_WrongCulture.ps1 ├── 6.4.3_Confirm.ps1 ├── 6.4.9_WhatIf.ps1 ├── 6.3.2_ParameterSetSelection.ps1 └── 6.5.14_UsingForce.ps1 ├── Chapter04 ├── 1.5.1.Division.ps1 ├── 1.6.1.Remainder.ps1 ├── 1.7.1.LeftShift.ps1 ├── 5.2.1.BinaryOr.ps1 ├── 5.4.1.BinaryNot.ps1 ├── 1.2.6.JoinArray.ps1 ├── 1.4.1.Multiplication.ps1 ├── 5.1.1.BinaryAnd.ps1 ├── 1.3.1.Subtraction.ps1 ├── 3.1.1.Equals.ps1 ├── 5.3.1.BinaryExclusiveOr.ps1 ├── 9.2.1.Comma.ps1 ├── 1.2.1.Addition_Simple.ps1 ├── 1.4.2.Multiplication_Strings.ps1 ├── 2.1.3.AddAndAssign.ps1 ├── 8.4.1.RedirectToNull.ps1 ├── 1.1.3.OperatorPrecedence_Parens.ps1 ├── 1.2.2.Concatenation.ps1 ├── 1.2.8.JoinHashtable.ps1 ├── 2.1.1.Assignment.ps1 ├── 2.1.12.SubtractAndAssign.ps1 ├── 2.2.1.MultiplyAndAssign.ps1 ├── 9.2.2.SingleElementArray.ps1 ├── 1.1.2.OperatorPrecedence_Shift.ps1 ├── 1.2.3.Concatenation_casting.ps1 ├── 2.1.9.AddToArray.ps1 ├── 3.1.2.CaseSensitiveEquals.ps1 ├── 7.1.1.As.ps1 ├── 8.4.2.RedirectToNull_EmptyString.ps1 ├── 9.1.1.Call.ps1 ├── 9.3.1.Format.ps1 ├── 9.3.4.Format_Currency.ps1 ├── 2.1.10.AddArrayToArray.ps1 ├── 2.1.4.AddAndAssign_Equivalent.ps1 ├── 2.2.5.DivideAndAssign.ps1 ├── 9.3.2.Format_Percentage.ps1 ├── 9.3.3.Format_Hex.ps1 ├── 2.1.8.CastToString.ps1 ├── 2.2.6.RemainderAndAssign.ps1 ├── 3.1.3.ExplicitCaseInsensitiveEquals.ps1 ├── 9.1.3.CallCommandAsString.ps1 ├── 2.1.5.ConcatenateAndAssign.ps1 ├── 2.2.2.MultiplyStringAndAssign.ps1 ├── 2.2.4.MultiplyArrayAndAssign.ps1 ├── 1.7.2.RightShift.ps1 ├── 1.7.3.shl_OutOfRange.ps1 ├── 1.7.7.shl_IPAddress.ps1 ├── 2.1.2.Assignment_Property.ps1 ├── 1.7.5.shl_Int16.ps1 ├── 1.7.6.shl_SIgning.ps1 ├── 2.1.11.AddToHashtable.ps1 ├── 3.3.2.NotEqual_Examples.ps1 ├── 3.6.2.In.ps1 ├── 4.2.3.ReplaceWithTokens.ps1 ├── 9.1.2.CallScriptBlock.ps1 ├── 1.4.4.ArrayMultiplication.ps1 ├── 1.7.4.shl_OutOfRange_Byte.ps1 ├── 4.2.1.Replace.ps1 ├── 1.1.1.OperatorPrecedence.ps1 ├── 3.2.2.ArrayEqualsNull.ps1 ├── 4.2.2.ReplaceWithNothing.ps1 ├── 6.4.1.Not.ps1 ├── 8.4.3.RedirectMultipleToNull.ps1 ├── 9.4.1.PostIncrement.ps1 ├── 9.4.2.PostDecrement.ps1 ├── 7.2.1.IsAndIsNot.ps1 ├── 3.3.1.Equal_Examples.ps1 ├── 3.6.1.ContainsAndNotContains.ps1 ├── 4.3.1.Split.ps1 ├── 9.5.1.Join.ps1 ├── 1.6.2.Remainder_Alternation.ps1 ├── 3.2.4.ImplicitBoolean.ps1 ├── 3.2.5.NullEqualsArray.ps1 ├── 6.3.1.ExclusiveOr.ps1 ├── 4.1.1.Match.ps1 ├── 5.3.2.BinaryExclusiveOr_Toggle.ps1 ├── 9.5.2.JoinNoSeparator.ps1 ├── 1.2.7.JoinArray_Examples.ps1 ├── 3.4.1.LikeAndNotLike.ps1 ├── 6.2.1.Or.ps1 ├── 3.5.1.GreaterThanAndLessThan.ps1 ├── 3.5.3.GreaterThanAndLessThan_Alphabetical_CaseSensitive.ps1 ├── 6.1.1.And.ps1 ├── 3.5.2.GreaterThanAndLessThan_Alphabetical_CaseInsensitive.ps1 ├── 3.2.3.ArrayEqualsNull_InvalidComparison.ps1 ├── 3.2.1.ComparisonAndArrays.ps1 └── 9.4.4.PreIncrementExample.ps1 ├── Chapter02 ├── 1.2.1_Get-Module.ps1 ├── 4.1.1_Get-PSSnapIn.ps1 ├── 1.3.1_PSMODULEPATH.ps1 ├── 2.1.3_Get-Command.ps1 ├── 2.3.2_Find-Module_Filter.ps1 ├── 4.2.1_Add-PSSnapIn.ps1 ├── 1.3.3_PSMODULEPATH_PSCore.ps1 ├── 1.2.2_Get-Module_ListAvailable.ps1 ├── 2.6.1_Save-Module.ps1 ├── 2.4.4_Install-Module_Reinstall.ps1 ├── 4.2.3_Get-Command_SnapIn.ps1 ├── 1.3.4_Import-Module_PSCore.ps1 ├── 2.4.2_Install-Module_Scope.ps1 ├── 2.4.3_Install-Module_Scope_Force.ps1 ├── 2.1.4_Import-Module_MaximumVersion.ps1 ├── 2.4.1_Install-Module.ps1 ├── 3.1.1_InstallWindowsCompatibility.ps1 ├── 2.3.1_Find-Module.ps1 ├── 1.1.1_New-Module.ps1 └── 2.1.2_Import-Module.ps1 ├── Chapter05 ├── 9.3.3.PeekMethod.ps1 ├── 9.4.3.PeekMethod.ps1 ├── 7.1.1.CreateArray.ps1 ├── 8.3.1.AccessKey.ps1 ├── 8.4.2.KeysProperty.ps1 ├── 9.1.3.AddMethod.ps1 ├── 1.1.6.AssignmentMultiple.ps1 ├── 6.1.1.ValueTypeAssignment.ps1 ├── 8.4.3.ValuesProperty.ps1 ├── 9.2.11.KeysProperty.ps1 ├── 9.2.12.ValuesProperty.ps1 ├── 9.3.2.EnumerateQueue.ps1 ├── 9.4.2.EnumerateStack.ps1 ├── 7.7.1.SetVariablesFromArray.ps1 ├── 8.1.1.CreateEmptyHashtable.ps1 ├── 9.2.5.StronglyTypedKey.ps1 ├── 2.3.2.New-Variable_Equivalent.ps1 ├── 3.1.1.VariableProvider.ps1 ├── 7.4.2.IndexFromEnd.ps1 ├── 7.4.4.ArrayOfIndexes.ps1 ├── 7.6.6.RemovingElements_ByIndex.ps1 ├── 4.2.1.Scope_Private.ps1 ├── 7.4.3.IndexRange.ps1 ├── 7.7.4.SetVariablesFromArray_Underrun.ps1 ├── 8.3.2.AccessKey_Alternative.ps1 ├── 7.2.1.ArrayOfType.ps1 ├── 7.3.1.AddElement.ps1 ├── 7.4.6.ArrayOfIndexes_FirstMustBeArray_Cause.ps1 ├── 7.5.1.Updating.ps1 ├── 7.6.10.RemovingElements_ByValue_Start.ps1 ├── 7.6.14.Clear.ps1 ├── 8.3.3.AccessKey_IntegerKey.ps1 ├── 9.3.5.DequeueMethod.ps1 ├── 1.1.2.VariableWithCurlyBraces.ps1 ├── 2.2.1.Get-Variable.ps1 ├── 2.3.1.New-Variable.ps1 ├── 7.3.2.AddElement_Equivalent.ps1 ├── 7.7.3.SetVariablesFromArray_Overflow.ps1 ├── 9.1.10.RemoveAll.ps1 ├── 9.2.2.AddMethod.ps1 ├── 9.2.4.PropertyNotation.ps1 ├── 9.4.5.PopMethod.ps1 ├── 2.5.2.Set-Variable_Private.ps1 ├── 3.1.2.VariableProvider_Test-Path.ps1 ├── 3.1.3.VariableProvider_Set-Item.ps1 ├── 7.6.12.RemovingElements_ByValue_IndexOf.ps1 ├── 8.0.1.CommandsUsingHashtables.ps1 ├── 8.2.1.AddMethod.ps1 ├── 9.1.1.List.ps1 ├── 9.1.2.ArrayList.ps1 ├── 9.1.6.FindIndex.ps1 ├── 3.1.4.VariableProvider_Get-Content.ps1 ├── 8.3.4.AccessKey_IntegerKey_Failure.ps1 ├── 9.1.4.InsertMethod.ps1 ├── 9.3.1.Queue.ps1 ├── 7.1.3.ArrayOfElements.ps1 ├── 7.1.6.ArrayOfElements_MixedType.ps1 ├── 8.1.2.CreateHashtableWithKeys.ps1 ├── 9.4.1.Stack.ps1 ├── 6.1.2.ValueTypeAssignment_Deviation.ps1 ├── 8.4.1.Start.ps1 ├── 7.1.4.ArrayOfElements_Alternate.ps1 ├── 7.4.1.GetByIndex.ps1 ├── 7.6.4.RemovingElements_NullElements_Pipeline.ps1 ├── 7.6.7.RemovingElements_ByIndex_NewArray.ps1 ├── 8.5.3.ClearMethod.ps1 ├── 1.1.1.VariableNames.ps1 ├── 2.4.1.Remove-Variable.ps1 ├── 2.5.1.Set-Variable.ps1 ├── 2.6.5.VariableProvider_Get-Item.ps1 ├── 7.6.11.RemovingElements_ByValue_Where-Object.ps1 ├── 8.5.1.RemoveMethod.ps1 ├── 7.4.7.ArrayOfIndexes_FirstMustBeArray_Fix.ps1 ├── 7.6.1.RemovingElements_NullElements.ps1 ├── 7.6.2.RemovingElements_NullElements_Count.ps1 ├── 7.6.3.RemovingElements_NullElements_Count.ps1 ├── 7.8.5.JaggedArray_AccessingElements.ps1 ├── 9.2.9.AccessByKey_StronglyTypedKey_Usage.ps1 ├── 9.3.4.EnqueueMethod.ps1 ├── 5.1.1.TypeConversion_ToString.ps1 ├── 6.1.3.ReferenceTypeAssignment.ps1 ├── 7.8.2.MultiDimensionalArray_AccessingElements.ps1 ├── 7.8.3.MultiDimensionalArray_AccessingElements_2.ps1 ├── 9.2.7.AccessByKey.ps1 ├── 1.1.3.Interpolation.ps1 ├── 5.1.3.AssignedType.ps1 ├── 7.6.5.RemovingElements_NullElements_Where-Object.ps1 ├── 7.8.1.MultiDimensionalArray.ps1 ├── 8.1.3.CreateHashtableWithKeys_Multiline.ps1 ├── 5.1.2.TypeConversion_ToDateTime.ps1 ├── 7.8.4.JaggedArray.ps1 ├── 8.4.4.Loop.ps1 ├── 9.4.4.PushMethod.ps1 ├── 7.1.5.ArrayOfElements_Multiline.ps1 ├── 7.3.3.AddElement_Equivalent.ps1 ├── 7.5.2.UpdatingInLoop.ps1 ├── 9.2.13.Loop.ps1 ├── 8.2.3.ContainsMethod.ps1 ├── 8.2.7.AddOnly.ps1 ├── 9.2.14.RemoveMethod.ps1 ├── 9.2.3.ContainsKey.ps1 ├── 1.1.5.Assignment.ps1 ├── 7.7.2.SetVariablesFromArray_Split.ps1 ├── 9.1.11.Reassign.ps1 ├── 8.2.6.UpdateOnly.ps1 ├── 9.1.7.IndexOf.ps1 ├── 6.1.4.ReferenceTypeAssignment_PropertyChange.ps1 ├── 7.6.9.RemovingElements_UsingLoop.ps1 ├── 8.2.4.ImplicitAdd.ps1 ├── 1.1.4.FileSystemProvider.ps1 ├── 9.1.5.AddRange_And_Index.ps1 ├── 8.2.5.ImplicitAdd_PropertyNotation.ps1 ├── 7.6.13.RemovingElements_ByValue_IndexOf_New.ps1 ├── 9.2.1.Dictionary.ps1 ├── 7.1.2.CreateArrayOfSize.ps1 ├── 8.2.9.KeysArray.ps1 ├── 2.4.2.Remove-Variable_ReferenceOnly.ps1 └── 9.2.8.AccessByKey_StronglyTypedKey.ps1 ├── Chapter08 ├── 3.2.6.Math_Abs.ps1 ├── 5.3.2.Get-Date_StartOfDay.ps1 ├── 3.2.8.Math_Sqrt.ps1 ├── 3.2.3.Math_RoundMethod.ps1 ├── 3.2.7.Math_Pow.ps1 ├── 1.1.3.StringSplitMethodOnArray.ps1 ├── 5.3.5.Get-Date_ToUniversalTime.ps1 ├── 1.1.2.StringTrimMethodOnArray.ps1 ├── 1.3.1.Split.ps1 ├── 5.3.1.Get-Date_And_New-TImeSpan.ps1 ├── 5.4.3.Get-Date_StoringDates.ps1 ├── 1.6.2.Remove.ps1 ├── 3.2.2.Hexadecimal.ps1 ├── 5.1.3.Test-DateTime_Get-Date.ps1 ├── 5.3.7.Get-Date_UtcKind.ps1 ├── 1.11.1.MethodChaining.ps1 ├── 1.3.7.SplitChoosesCharArray.ps1 ├── 2.2.4.ConvertFrom-Csv_FirstLineIsHeader.ps1 ├── 1.4.1.Replace.ps1 ├── 1.9.4.ToTitleCase_Alternative.ps1 ├── 2.2.2.ConvertFrom-Csv.ps1 ├── 4.0.2.Convert_ToInt32_FromBinary.ps1 ├── 4.0.3.Convert_ToInt32_FromHexadecimal.ps1 ├── 1.10.1.Contains.ps1 ├── 1.3.6.CharArray.ps1 ├── 1.9.1.ToUpperAndToLower.ps1 ├── 3.1.1.ByteMultiplicationOperators.ps1 ├── 3.2.9.Math_PiAndE.ps1 ├── 5.5.3.ComparingDates_RightHandSideCasting_USFormat.ps1 ├── 1.1.5.AddDaysMethodArrayOfDateTime.ps1 ├── 1.1.6.DayOfWeekPropertyArrayOfDateTime.ps1 ├── 1.10.2.StartsWithAndEndsWith.ps1 ├── 1.5.1.Trim.ps1 ├── 3.2.1.PowerOf10Operator.ps1 ├── 1.5.2.Trim_ArrayOfChar.ps1 ├── 5.4.2.Get-Date_MethodChaining.ps1 ├── 3.2.5.Math_CeilingAndFloor.ps1 ├── 5.5.1.ComparingDates.ps1 ├── 1.3.8.ForceSplitString.ps1 ├── 1.9.2.ToLowerAndReplace.ps1 ├── 2.3.1.Convert-String.ps1 ├── 5.3.3.Get-Date_AddDays.ps1 ├── 5.4.1.Get-Date_Format_And_ToString.ps1 ├── 1.7.2.IndexOfAndIf.ps1 ├── 1.9.3.ToTitleCase.ps1 ├── 1.2.1.Substring.ps1 ├── 1.5.4.TrimEndTooFar.ps1 ├── 2.3.2.Convert-String_AbstractExample.ps1 ├── 1.2.2.Substring_StartAndLength.ps1 ├── 1.8.2.PadRight.ps1 ├── 1.8.3.PadLeft.ps1 ├── 5.5.2.ComparingDates_RightHandSideCasting.ps1 ├── 1.6.1.Insert.ps1 ├── 2.1.1.ToBase64String.ps1 ├── 2.1.3.ToBase64String_CharArray.ps1 ├── 1.4.2.Replace_vs_-replace.ps1 ├── 3.1.2.ByteMultiplicationOperatorsAndFormat.ps1 ├── 1.3.2.Split_EmptyEntries.ps1 ├── 1.3.4.Split_RemoveEmptyEntries_Assign.ps1 ├── 2.2.5.Export-CsvAndImport-Csv.ps1 ├── 5.1.2.Test-DateTime.ps1 ├── 5.2.1.DateTime_ParseExact.ps1 ├── 1.3.3.Split_RemoveEmptyEntries.ps1 ├── 2.3.3.Convert-String_Hashtable.ps1 ├── 5.1.1.CastToDateTime.ps1 ├── 1.7.1.IndexOfAndLastIndexOf.ps1 ├── 2.2.3.ConvertFrom-Csv_MultilineField.ps1 ├── 1.5.3.Trim_Overloads.ps1 ├── 2.1.2.ToBase64String_ASCII.ps1 ├── 4.0.1.Casting.ps1 ├── 1.1.1.IndexIntoString.ps1 ├── 3.2.4.Math_RoundMethodMidpointRounding.ps1 └── 2.1.4.ToBase64String_Unicode.ps1 ├── Chapter10 ├── 6.1.1_Get-PSProvider.ps1 ├── 6.1.3_Undo-Transaction.ps1 ├── 3.2.1_FileAttributes.ps1 ├── 5.4.5_Get-Acl_Error.ps1 ├── 6.2.4_Complete-Transaction.ps1 ├── 1.2.2_Get-Item_Error.ps1 ├── 2.1.1_Test-Path.ps1 ├── 1.2.3_Get-Item_Force.ps1 ├── 5.2.1_Get-Acl_Owner.ps1 ├── 5.3.1_Get-Acl_Audit.ps1 ├── 3.1.2_Get-Item_SetProperty.ps1 ├── 3.2.2_FileAttribute_eq.ps1 ├── 6.3.5_Get-Command_UseTransaction.ps1 ├── 5.8.1_RightsEnum.ps1 ├── 4.1.3_Remove-ItemProperty.ps1 ├── 3.2.3_FileAttribute_replace.ps1 ├── 5.7.2_CopyAcl.ps1 ├── 3.2.8_FileAttributes_AsString_Explain.ps1 ├── 4.1.2_Set-ItemProperty.ps1 ├── 7.2.1_Test-FileCatalog.ps1 ├── 7.1.1_New-FileCatalog.ps1 ├── 3.2.5_FileAttribute_bor.ps1 ├── 7.2.5_Test-FileCatalog_Removed.ps1 ├── 2.2.3_Remove-Item.ps1 ├── 3.2.4_FileAttributes_bxor.ps1 ├── 3.2.7_FileAttribute_AsString.ps1 ├── 7.2.4_Test-FileCatalog_Added.ps1 ├── 2.1.2_Test-Path_PathType.ps1 ├── 3.1.1_Set-ItemProperty.ps1 ├── 4.1.4_New-ItemProperty.ps1 ├── 5.6.3_Get-Acl.ps1 ├── 5.8.4_AddAccessRule.ps1 ├── 7.2.6_Test-FileCatalog_Modified.ps1 ├── 5.4.6_Set-Acl_Fix.ps1 ├── 1.3.1_New-PSDrive.ps1 ├── 5.4.3_Get-Acl.ps1 ├── 5.6.1_Set-Acl_Setup_1.ps1 ├── 5.6.2_Set-Acl_Setup_2.ps1 ├── 2.1.3_Test-Path_If.ps1 ├── 3.2.6_FileAttributes_assign.ps1 ├── 5.4.1_Set-Acl_Protection_Enable.ps1 ├── 5.4.2_Set-Acl_Protection_Disable.ps1 ├── 5.4.4_Set-Acl_Protection_Discard.ps1 ├── 5.10.1_GenericAccessRights.ps1 ├── 5.2.2_Set-Acl_Owner.ps1 ├── 2.2.1_New-Item.ps1 ├── 5.9.2_AddAccessRule.ps1 └── 4.1.1_Get-ItemProperty.ps1 ├── Chapter14 ├── 1.4.9_Test-WSMan.ps1 ├── 1.1.1_Disable-Remoting.ps1 ├── 2.4.1_Enter-PSSession.ps1 ├── 1.3.2_Restart_winrm.ps1 ├── 1.4.4_Set-WSManQuickConfig.ps1 ├── 3.1.2_chsh.ps1 ├── 5.1.3_Enable-WSManCredSSP_Server.ps1 ├── 6.1.1_CimSessionCommands.ps1 ├── 1.5.4_ReadAndWrite.ps1 ├── 1.7.1_GetTrustedHosts.ps1 ├── 4.1.1_InstallOpenSSH.ps1 ├── 5.1.4_Disable-WSManCredSSP_Client.ps1 ├── 5.1.5_Disable-WSManCredSSP_Server.ps1 ├── 1.3.1_Get-WSManInstance.ps1 ├── 2.6.2_Import-Module_ExportedSession.ps1 ├── 1.5.1_Get-PSSessionConfiguration.ps1 ├── 2.2.2_SessionsAreGlobal.ps1 ├── 3.1.3_yum.ps1 ├── 1.2.1_Get-WSManInstance.ps1 ├── 4.1.3_GetPublicKey.ps1 ├── 5.1.2_Enable-WSManCredSSP.ps1 ├── 3.1.1_ListShells.ps1 ├── 2.3.10_Reconnect.ps1 ├── 2.4.2_Enter-PSSession_Session.ps1 ├── 2.3.2_Invoke-Command_MultipleComputers.ps1 ├── 5.1.1_New-PSSession_CredSSP.ps1 ├── 2.3.7_InDisconnectedSession.ps1 ├── 1.4.8_SkipCACheck.ps1 ├── 1.5.6_SIDTranslate.ps1 ├── 1.5.8_SetAcl.ps1 ├── 1.5.2_Set-PSSessionConfiguration_ShowSecurityDescriptorUI.ps1 ├── 2.3.1_Invoke-Command.ps1 ├── 2.3.8_InDisconnectedSession_ConnectAndReceive.ps1 ├── 2.7.1_Copy-Item_FromSession.ps1 ├── 2.7.2_Copy-Item_ToSession.ps1 ├── 2.3.5_AsJob.ps1 ├── 3.1.4_firewall-cmd.ps1 ├── 4.2.5_GetPublicKey.ps1 ├── 2.5.1_Import-PSSession.ps1 ├── 5.2.1_PassingCredentials.ps1 ├── 4.2.2_InstallSSHD.ps1 ├── 4.2.6_AddToAuthorizedKeys.ps1 └── 2.3.9_CreateAndDisconnect.ps1 ├── Chapter21 ├── 3.4.1_throw.ps1 ├── 3.6.1_BuiltInErrorVariable.ps1 ├── 3.6.2_BuiltInErrorVariable_Index.ps1 ├── 3.1.1_ErrorRecord.ps1 ├── 3.2.1_Write-Error_Message.ps1 ├── 3.5.1_throw_ExceptionType.ps1 ├── 5.3.1_Connect-Server.ps1 ├── 3.5.2_throw_ArgumentException.ps1 ├── 4.1.1_TryCatch.ps1 ├── 3.1.2_ScriptStackTrace.ps1 ├── 3.2.2_Write-Error_CategoryAndErrorId.ps1 ├── 4.2.1_Re-throw.ps1 ├── 5.2.3_WithErrorActionSilentlyContinue.ps1 ├── 5.2.1_DemoFunction.ps1 ├── 5.5.1_Trap.ps1 ├── 3.2.3_Write-Error_Loop.ps1 ├── 5.1.3_Cmdlet_Exceptions.ps1 ├── 3.1.5_UsingTargetObject.ps1 ├── 5.1.4_Method_Exceptions.ps1 ├── 4.2.3_Re-throw_ErrorVariable.ps1 ├── 5.5.2_Trap_Terminating.ps1 └── 4.2.7_Catch_InnerException_ArgumentOutOfRangeException.ps1 ├── Chapter03 ├── 2.3.5.Method_close.ps1 ├── 2.2.2.Property_Parens.ps1 ├── 7.1.1.Export-Csv.ps1 ├── 7.3.2.Import-CliXml.ps1 ├── 2.2.1.Property.ps1 ├── 4.2.3.Sort-Object_Property.ps1 ├── 7.2.1.Import-Csv.ps1 ├── 2.3.8.Method_chaining.ps1 ├── 3.2.2.ForEach-Object_MemberExpansion.ps1 ├── 4.1.5.Select-Object_Last.ps1 ├── 1.1.1.StdOut.ps1 ├── 4.1.1.Select-Object.ps1 ├── 4.1.6.Select-Object_Skip.ps1 ├── 4.1.8.Select-Object_Unique.ps1 ├── 2.2.4.Property_Sub-property_Parens.ps1 ├── 2.3.4.Method_results.ps1 ├── 3.3.1.Where-Object.ps1 ├── 4.1.4.Select-Object_First.ps1 ├── 6.1.1.Compare-Object.ps1 ├── 4.1.2.Select-Object_Wildcards.ps1 ├── 2.1.1.Get-Member.ps1 ├── 2.2.3.Property_Sub-property.ps1 ├── 2.4.4.AccessModifiers_Result.ps1 ├── 4.1.3.Select-Object_Exclude_Wildcards.ps1 ├── 7.1.4.Export-Csv_NoTypeInformation.ps1 ├── 3.3.2.Where-Object_OddOrder.ps1 ├── 3.2.1.ForEach-Object.ps1 ├── 4.2.4.Sort-Object_MultipleProperties.ps1 ├── 5.1.3.Group-Object_Property.ps1 ├── 2.3.3.Method_arguments.ps1 ├── 2.3.6.Method_ToString.ps1 ├── 4.2.1.Sort-Object.ps1 ├── 2.1.2.Get-Member_MemberType.ps1 ├── 2.3.7.Get-Date_Format_alternative.ps1 ├── 2.4.3.AccessModifiers_Set.ps1 ├── 3.3.3.Where-Object_FilterScript.ps1 ├── 2.3.2.Method_invoke.ps1 ├── 4.2.2.Sort-Object_CaseInsensitive.ps1 ├── 1.3.1.StdOut.ps1 ├── 2.5.1.Add-Member_NoteProperty.ps1 ├── 7.3.1.Export-CliXml.ps1 ├── 2.2.5.Property_with_spaces.ps1 ├── 1.2.1.StdOut.ps1 ├── 4.1.12.Select-Object_CalculatedProperties.ps1 ├── 5.1.7.Group-Object_AsHashtable.ps1 ├── 2.2.6.Property_with_variable.ps1 ├── 7.2.4.ConvertFrom-Csv.ps1 ├── 7.1.3.Export-Csv_Append.ps1 ├── 3.2.3.ForEach-Object_MemberInvocation.ps1 ├── 4.1.7.Select-Object_ExpandProperty.ps1 ├── 5.2.1.Measure-Object.ps1 ├── 5.1.2.Group-Object_NoElement.ps1 ├── 6.1.4.Compare-Object_ExcludeDifferent_PassThru.ps1 └── 7.1.7.ConvertTo-Csv_Arrays_Why.ps1 ├── Chapter11 ├── 4.4.3_WmiClass.ps1 ├── 5.1.13_AddToDACL.ps1 ├── 4.4.1_Get-WmiObject_Class.ps1 ├── 4.2.1_Get-WmiObject.ps1 ├── 4.4.2_Get-WmiObject_Class_Recurse.ps1 ├── 4.2.2_WmiSearcher.ps1 ├── 3.2.2_Get-CimInstance_Namespace.ps1 ├── 4.7.1_CreateInstance.ps1 ├── 2.1.4_WQL_Wildcards.ps1 ├── 2.2.3_Wmi_accelerator.ps1 ├── 3.1.3_Get-CimInstance_Property.ps1 ├── 2.1.5_Quoting_Outer_Single.ps1 ├── 2.1.6_Quoting_Outer_Double.ps1 ├── 2.2.4_ASSOCIATORS_OF.ps1 ├── 5.1.8_AceFlags.ps1 ├── 5.3.2_SetFromSDDL.ps1 ├── 2.1.2_WQL_SELECT_WHERE.ps1 ├── 2.1.3_WQL_Escape_characters.ps1 ├── 3.5.2_CimInstance_new.ps1 ├── 5.1.3_ShareSecurity.ps1 ├── 2.2.2_Wmi_object_path.ps1 ├── 5.1.4_Descriptor.ps1 ├── 5.2.1_WmiPermissions.ps1 ├── 3.5.1_New-CimInstance.ps1 ├── 2.3.5_ASSOCIATORS_OF_ResultClass.ps1 ├── 5.1.14_SetDescriptor.ps1 ├── 5.1.9_FileSystemRights.ps1 ├── 4.1.1_Wmi_and_WmiClass.ps1 ├── 2.1.1_WQL_SELECT.ps1 ├── 3.1.4_Get-CimInstance_Query.ps1 ├── 3.1.1_Get-CimInstance.ps1 ├── 5.1.11_CreateTrustee.ps1 ├── 2.1.7_Interpolation_and_sub-expressions.ps1 └── 3.3.4_Invoke-CimMethod_output.ps1 ├── Chapter20 ├── 1.1.1_Ast.ps1 ├── 2.8.1_Mock.ps1 ├── 2.7.2_TestDrive_AfterAll.ps1 ├── 1.3.1_PSScriptAnalyzer.ps1 ├── 2.10.1_SimpleObject.ps1 ├── 2.9.6_Assert-MockCalled_Times_0.ps1 ├── 1.3.3_Invoke-ScriptAnalyzer.ps1 ├── 2.10.16_New-CimInstance_WithoutNamespace.ps1 ├── 2.9.7_Assert-MockCalled_Times_Exactly.ps1 ├── 2.9.8_ParameterFilter.ps1 ├── 2.10.2_SimpleMethod.ps1 ├── 2.6.3_AfterAll.ps1 ├── 1.1.2_Ast_ScriptBlock.ps1 ├── 1.1.4_Get-Item.ps1 ├── 2.10.15_New-CimInstance_WithNamespace.ps1 ├── 2.9.4_DemoFunction.ps1 ├── 2.8.2_Mock_MockWith.ps1 ├── 1.1.3_Get-Command_Definition.ps1 ├── 2.7.1_TestDrive_BeforeAll.ps1 ├── 2.9.3_Mock_MockWith_Parameter.ps1 ├── 2.9.12_GetParamBlock.ps1 ├── 2.11.2_Mocks.ps1 ├── 2.3.3_IndependentVerification_Error.ps1 └── 2.6.2_BeforeAll.ps1 ├── Chapter07 ├── 8.1.5.Accelerators_Get.ps1 ├── 9.1.3.gacutil.ps1 ├── 8.1.1.TypeAccelerators_PowerShell.ps1 ├── 9.1.1.UsingAssembly.ps1 ├── 6.2.2.StaticPropertyInvocation.ps1 ├── 6.2.4.StaticFields.ps1 ├── 9.1.2.Add-Type.ps1 ├── 1.1.1.GetAssemblies.ps1 ├── 5.2.1.newMethod.ps1 ├── 5.2.2.New-Object.ps1 ├── 9.1.7.UsingAssemblyPath.ps1 ├── 9.2.1.UsingNamespace.ps1 ├── 1.1.2.GetAssemblyForType.ps1 ├── 9.2.2.UsingNamespace_Result.ps1 ├── 5.2.14.New-Object_Fix.ps1 ├── 2.1.1.GetAssemblies_ImplicitSystemNamespace.ps1 ├── 2.1.2.GetAssemblies_ImplicitSystemNamespace_2.ps1 ├── 6.3.4.StaticMethodInvocation_Arguments.ps1 ├── 5.2.5.Alternatives.ps1 ├── 6.3.2.StaticMethodInvocation.ps1 ├── 5.2.15.ChainCasting.ps1 ├── 5.2.13.newMethod_ArgumentList_Working.ps1 ├── 6.2.3.StaticPropertyInvocation_FromVariable.ps1 ├── 8.1.2.TypeAccelerators_ADSI.ps1 ├── 8.1.3.TypeAccelerators_Listing.ps1 ├── 9.1.5.UsingAssembly_Specific.ps1 ├── 1.1.3.GetAssemblyForType_Others.ps1 ├── 6.3.5.StaticMethodInvocation_ArgumentList_Failure.ps1 ├── 6.3.6.StaticMethodInvocation_ArgumentList_InvokeMethod.ps1 ├── 8.1.4.TypeAccelerators_Adding.ps1 ├── 6.3.3.StaticMethodInvocation_FromVariable.ps1 ├── 5.2.6.ContructorSelection.ps1 ├── 9.2.4.UsingMultipleNamespaces.ps1 ├── 5.2.9.ArgumentsAsAnArray.ps1 ├── 5.3.1.CreationFromHashtable_Constructor.ps1 └── 5.3.2.CreationFromHashtable.ps1 ├── Chapter09 ├── 1.3.1.AnyCharacter.ps1 ├── 1.5.1.EscapeCharacter.ps1 ├── 6.1.2.Repetition_Groups.ps1 ├── 6.1.3.Repetition_ZeroOrMore.ps1 ├── 1.5.4.ValidButInvalid.ps1 ├── 1.5.5.LiteralPeriod.ps1 ├── 6.1.1.Repetition_Start.ps1 ├── 1.3.2.AnyCharacter_NoAnchors.ps1 ├── 1.6.1.OptionalCharacters.ps1 ├── 2.1.0.StartOfString.ps1 ├── 4.1.1.CharacterClasses.ps1 ├── 4.1.10.Range_LIteralHyphen_Escaped.ps1 ├── 4.1.9.Range_LIteralHyphen_Alternative.ps1 ├── 4.3.1.ClassSubtraction_Start.ps1 ├── 1.4.1.Repetition_ZeroOrMore.ps1 ├── 5.1.1.Alternation.ps1 ├── 1.4.2.Repetition_ZeroOrMore_Zero.ps1 ├── 2.1.1.EndOfString.ps1 ├── 4.2.1.NegatedCharacterClass.ps1 ├── 4.1.7.Range_VersionString.ps1 ├── 1.5.3.EscapingTheEscapeCharacter_WithReplace.ps1 ├── 4.3.3.ClassSubtraction_UsingSubtraction.ps1 ├── 1.2.1.SpecialCharacters.ps1 ├── 1.5.6.RemoveAllBeforeCharacter.ps1 ├── 1.5.7.RemoveAllAfterCharacter.ps1 ├── 4.1.5.Range_HexadecimalNumber_CaseSensitive.ps1 ├── 4.3.2.ClassSubtraction_UsingSpecificRanges.ps1 ├── 1.5.2.EscapingTheEscapeCharacter.ps1 ├── 1.4.3.Repetition_OneOrMore.ps1 ├── 4.1.2.Range.ps1 ├── 5.1.3.Alternation_WIthAnchors.ps1 ├── 2.1.2.ReplaceTooMuch.ps1 ├── 5.1.4.Alternation_WIthAnchors_AndGroup.ps1 ├── 2.1.3.WordBoundary.ps1 ├── 4.1.8.Range_LIteralHyphen.ps1 ├── 6.1.6.Alternation_WordList.ps1 ├── 8.2.1.MatchIPAddress.ps1 ├── 4.1.3.Range_IsASCII.ps1 ├── 7.1.3.CaptureGroups_Tokens.ps1 ├── 8.2.2.UseIPAddressTryParse.ps1 ├── 7.3.3.NonCapturingGroup_Replace.ps1 ├── 3.1.2.LazyQuanifier.ps1 ├── 4.1.6.Range_SimpleIPAddress.ps1 ├── 1.4.4.RepetitionAndPeriod.ps1 ├── 3.1.1.GreedyQuanifier.ps1 ├── 4.1.4.Range_HexadecimalNumber.ps1 └── 5.1.2.Alternation_Example.ps1 ├── Chapter12 ├── 4.4.3_Save.ps1 ├── 1.4.1_System.Web.ps1 ├── 4.1.1_Add-Type.ps1 ├── 4.1.2_using_assembly.ps1 ├── 3.10.2_RemoveAttribute.ps1 ├── 4.5.1_XNamespace.ps1 ├── 5.2.1_ConvertFrom-Json.ps1 ├── 5.3.4_Remove-TypeData.ps1 ├── 4.2.2_Load.ps1 ├── 4.5.2_XNamespace_Shorthand.ps1 ├── 1.1.1_ConvertTo-Html.ps1 ├── 3.2.3_SelectNodes.ps1 ├── 3.7.3_SelectSingleNode_Set.ps1 ├── 3.11.1_ISEHelp.ps1 ├── 3.2.4_Select-Xml_FromFile.ps1 ├── 4.3.2_UsingXPath.ps1 ├── 3.7.2_ExampleXml.ps1 ├── 1.4.3_System.Web_HtmlDecode.ps1 ├── 1.3.2_ConvertTo-Html_Injecting_CSS.ps1 ├── 1.4.2_System.Web_HtmlEncode.ps1 ├── 3.9.3_OuterXml.ps1 ├── 3.9.2_ImportNode.ps1 ├── 4.4.2_ToString.ps1 └── 3.1.2_PropertyExpansion.ps1 ├── Chapter13 ├── 1.5.3_Certificate.ps1 ├── 1.5.5_ChainStatus.ps1 ├── 3.3.2_CreateObject.ps1 ├── 3.2.8_ExplicitEnumValue.ps1 ├── 1.9.9_OAUTH_token_header.ps1 ├── 2.1.1_Invoke-RestMethod.ps1 ├── 1.5.4_Chain.ps1 ├── 3.4.4_NamespaceProperty.ps1 ├── 1.1.1_Invoke-WebRequest.ps1 ├── 1.3.2_Invoke-WebRequest_InvalidCertificate_PSCore.ps1 ├── 1.6.2_SecurityProtocol.ps1 ├── 2.3.5_GetAuthorizationCode.ps1 ├── 2.3.10_UseToken.ps1 ├── 3.2.4_SimpleArguments.ps1 ├── 2.3.1_BasicAuthentication.ps1 ├── 3.1.2_New-WebServiceProxy.ps1 ├── 2.1.2_Invoke-RestMethod_UsingBody.ps1 ├── 2.3.4_OAUTH_Secrets.ps1 ├── 1.4.2_Invoke-WebRequest_CertificatePolicy.ps1 ├── 1.5.1_WebClient_ServicePointManager.ps1 └── 3.2.5_Enum.ps1 ├── Chapter15 ├── 3.5.5.RunspacePool_GC_Collect.ps1 ├── 3.7.2.SetMaxRunspaces.ps1 ├── 2.3.2.Unregister-Event.ps1 ├── 3.1.1.PowerShellInstance.ps1 ├── 3.5.1.CreateRunspacePool.ps1 ├── 3.1.4.Equivalent.ps1 ├── 3.6.1.InitialSessionState.ps1 ├── 2.4.3.Cleanup.ps1 ├── 3.4.3.Waiting.ps1 ├── 3.5.4.RunspacePool_CleaningUp.ps1 ├── 3.1.2.AddCommand.ps1 ├── 3.8.1.SynchronizedHashtable.ps1 ├── 3.8.3.SynchronizedArrayList.ps1 ├── 3.6.4.ImportPSModule.ps1 ├── 3.2.1.BeginInvoke.ps1 ├── 3.2.2.InvocationStateInfo.ps1 ├── 3.2.8.Stop.ps1 ├── 3.3.1.EndInvoke.ps1 ├── 3.6.5.ImportPSModule_Multiple.ps1 ├── 3.1.5.AddScript.ps1 ├── 3.2.7.OrphanedRunspace_InProgress.ps1 ├── 3.6.6.ImportPSSnapIn.ps1 ├── 3.7.1.InitialSessionState_and_RunspacePool.ps1 └── 1.3.1.Wait-Job_Alternative.ps1 ├── Chapter16 ├── 1.1.2_about_Requires.ps1 ├── 3.5.2_about_CommonParameters.ps1 ├── 1.4.1_about_comment_based_help.ps1 ├── 2.5.8_Cast_to_Void_Parentheses.ps1 ├── 3.2.3_Test-Nullable.ps1 ├── 1.2.1_Using_recap.ps1 ├── 1.5.1_line_break_on_pipe.ps1 ├── 3.5.1_CmdletBinding_empty_param.ps1 ├── 2.4.2_Invoke-Return_running.ps1 ├── 3.2.2_param_nullable.ps1 ├── 1.5.2_short_pipeline.ps1 ├── 2.1.2_Show-Pipeline_begin.ps1 ├── 1.5.3_line_break_on_logic_operator.ps1 ├── 3.5.3_Show-Verbose.ps1 ├── 1.4.8_CBH_example_code_parser.ps1 ├── 3.2.1_param_type.ps1 ├── 4.1.4_WhatIf_running.ps1 ├── 3.1.1_param.ps1 ├── 4.1.2_WhatIf_running.ps1 ├── 3.5.5_CmdletBinding_positional_binding.ps1 ├── 3.3.1_Default_value.ps1 ├── 2.2.2_Show-Pipeline_running.ps1 ├── 1.4.2_CBH_script.ps1 └── 3.3.4_Default_value_throw.ps1 ├── Chapter18 ├── 1.2.2.enum_CastToInt.ps1 ├── 1.4.2.enum_cast_to_Boolean.ps1 ├── 1.5.2.enum_flags_casting_string.ps1 ├── 1.3.1.enum_no_value.ps1 ├── 1.3.2.enum_sequencing.ps1 ├── 2.1.1.class.ps1 ├── 2.7.2.static_invocation.ps1 ├── 2.2.1.properties.ps1 ├── 2.2.3.properties_accessing.ps1 ├── 1.4.1.enum_vs_ValidateSet.ps1 ├── 1.1.1.enum.ps1 ├── 1.5.3.enum_flags_casting_int.ps1 ├── 2.1.2.class_new.ps1 ├── 1.1.2.enum_duplicate_values.ps1 ├── 1.5.1.enum_flags.ps1 ├── 2.3.1.constructors_default.ps1 ├── 2.3.2.constructors_explicit.ps1 ├── 3.1.4.DateTimeStringTransformationAttribute_result.ps1 ├── 1.3.3.enum_sequencing_skipping_and_restarting.ps1 ├── 1.6.1.enum_union.ps1 ├── 2.5.1.inheritance.ps1 ├── 2.5.2.inherited_members.ps1 ├── 2.7.1.static.ps1 ├── 2.5.3.inherited_override.ps1 ├── 5.5.4.Dsc_ComputerDescription_Get.ps1 ├── 1.2.3.enum_NewToPS62.ps1 ├── 1.5.4.enum_composite_flags.ps1 └── 5.5.3.Dsc_ComputerDescription_using_module.ps1 ├── Chapter06 ├── 1.1.4.BooleanCastFalse.ps1 ├── 2.1.1.foreach.ps1 ├── 1.1.3.BooleanCastTrue.ps1 ├── 1.1.5.IfAndAssign.ps1 ├── 1.1.2.ImplicitBoolean.ps1 ├── 2.2.2.for_Step.ps1 ├── 1.2.4.Switch_Array.ps1 ├── 2.4.1.while.ps1 ├── 2.2.3.for_Step_Decrement.ps1 ├── 2.2.1.for.ps1 ├── 1.2.1.Switch.ps1 ├── 1.1.6.IfAndAssign_Practical.ps1 ├── 1.2.2.Switch_Break.ps1 ├── 2.3.1.do_until.ps1 ├── 2.5.1.break.ps1 ├── 1.1.1.If.ps1 ├── 2.3.2.do_while.ps1 ├── 1.2.3.Switch_Default.ps1 ├── 3.1.3.foreach_goodpractice.ps1 └── 1.2.8.Switch_Expression.ps1 ├── Chapter17 ├── 2.3.4.AllowEmptyCollection_Demo.ps1 ├── 2.2.12.ValidatePattern_Options_CaseSensitive.ps1 ├── 2.2.11.ValidatePattern_Options_Multiple.ps1 ├── 3.1.5.ValueFromPipeine_Where-Object_Filtering.ps1 ├── 4.1.4.PositionAndParameterSetName.ps1 ├── 1.2.2.PositionalBinding.ps1 ├── 2.1.1.PSTypeNameAttribute.ps1 ├── 3.2.3.ValueFromPipelineByPropertyName_MultipleParams_Binding.ps1 ├── 4.1.3.DefaultParameterSetName.ps1 ├── 4.1.9.MultipleParameterSets_Exploration.ps1 ├── 2.1.4.PSTypeNameAttribute_Add-Member.ps1 ├── 3.1.9.ValueFromPipeine_MutlipleParams_Trace-Command.ps1 ├── 2.2.1.Exploration.ps1 ├── 2.4.1.PSReference.ps1 ├── 2.4.2.PSReference_ReferenceTypes.ps1 ├── 3.1.12.ValueFromPipeine_PSTypeName_CreatingTheType.ps1 ├── 2.1.6.PSTypeNameAttribute_Exploration.ps1 ├── 1.4.2.ValueFromRemainingArguments_UnboundArguments.ps1 ├── 2.2.2.ValidateNotNull.ps1 ├── 2.2.15.ValidateRange.ps1 ├── 2.2.6.ValidateCount.ps1 ├── 2.2.7.ValidateDrive.ps1 ├── 1.1.2.ParameterAttributeAndCmdletBinding.ps1 ├── 1.4.1.ValueFromRemainingArguments_BasicFunction.ps1 ├── 2.2.19.ValidateSet.ps1 ├── 2.2.8.ValidateLength.ps1 ├── 3.1.8.ValueFromPipeine_MutlipleParams_Usage.ps1 ├── 1.1.4.ParameterAttribute_BooleanShorthand.ps1 ├── 2.2.9.ValidatePattern.ps1 ├── 2.1.5.PSTypeNameAttribute_Validation.ps1 ├── 2.2.3.ValidateNotNull_AllowsEmpty.ps1 ├── 6.1.1.dynamicparam.ps1 ├── 6.2.2.RuntimeDefinedParameter_paramEquivalent.ps1 ├── 2.3.1.AllowNull.ps1 ├── 1.2.3.PositionalBinding_EffectivelyDisabled_Function.ps1 └── 2.2.16.ValidateScript.ps1 └── Chapter19 ├── 1.2.2.Export-ModuleMember_wildcards.ps1 ├── 2.2.2.ModuleBuilder_InModuleFolder.ps1 ├── 4.2.3.Remove-Module.ps1 ├── 1.1.1.LocalMachine_v1.psm1 ├── 3.2.3.ModuleService_InvokePrivateFunction.ps1 ├── 3.2.4.ModuleService_ListingCommands.ps1 ├── 2.2.1.ModuleBuilder.ps1 ├── 4.2.2.ShowGlobalVariable.ps1 └── 3.2.2.ModuleService_AccessingVariables.ps1 /Chapter01/4.2.1_Get-Help.ps1: -------------------------------------------------------------------------------- 1 | Get-Help -------------------------------------------------------------------------------- /Chapter01/5.1.1_Get-Verb.ps1: -------------------------------------------------------------------------------- 1 | Get-Verb -------------------------------------------------------------------------------- /Chapter01/5.4.1_Get-Alias.ps1: -------------------------------------------------------------------------------- 1 | Get-Alias -------------------------------------------------------------------------------- /Chapter04/1.5.1.Division.ps1: -------------------------------------------------------------------------------- 1 | 20 / 5 -------------------------------------------------------------------------------- /Chapter04/1.6.1.Remainder.ps1: -------------------------------------------------------------------------------- 1 | 3 % 2 -------------------------------------------------------------------------------- /Chapter04/1.7.1.LeftShift.ps1: -------------------------------------------------------------------------------- 1 | 78 -shl 1 -------------------------------------------------------------------------------- /Chapter04/5.2.1.BinaryOr.ps1: -------------------------------------------------------------------------------- 1 | 11 -bor 12 -------------------------------------------------------------------------------- /Chapter04/5.4.1.BinaryNot.ps1: -------------------------------------------------------------------------------- 1 | -bnot 122 -------------------------------------------------------------------------------- /Chapter01/4.4.1_Update-Help.ps1: -------------------------------------------------------------------------------- 1 | Update-Help -------------------------------------------------------------------------------- /Chapter01/7.2.1_Get-PSDrive.ps1: -------------------------------------------------------------------------------- 1 | Get-PSDrive -------------------------------------------------------------------------------- /Chapter02/1.2.1_Get-Module.ps1: -------------------------------------------------------------------------------- 1 | Get-Module -------------------------------------------------------------------------------- /Chapter04/1.2.6.JoinArray.ps1: -------------------------------------------------------------------------------- 1 | @(1, 2) + 3 -------------------------------------------------------------------------------- /Chapter04/1.4.1.Multiplication.ps1: -------------------------------------------------------------------------------- 1 | 2.5 * 2 -------------------------------------------------------------------------------- /Chapter04/5.1.1.BinaryAnd.ps1: -------------------------------------------------------------------------------- 1 | 11 -band 6 -------------------------------------------------------------------------------- /Chapter05/9.3.3.PeekMethod.ps1: -------------------------------------------------------------------------------- 1 | $queue.Peek() -------------------------------------------------------------------------------- /Chapter05/9.4.3.PeekMethod.ps1: -------------------------------------------------------------------------------- 1 | $stack.Peek() -------------------------------------------------------------------------------- /Chapter01/4.1.1_Get-Help.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Out-Null -------------------------------------------------------------------------------- /Chapter01/7.1.2_Get-PSProvider.ps1: -------------------------------------------------------------------------------- 1 | Get-PSProvider -------------------------------------------------------------------------------- /Chapter04/1.3.1.Subtraction.ps1: -------------------------------------------------------------------------------- 1 | 5 - 2 2 | 2 - 20 -------------------------------------------------------------------------------- /Chapter04/3.1.1.Equals.ps1: -------------------------------------------------------------------------------- 1 | 'Trees' -eq 'trees' -------------------------------------------------------------------------------- /Chapter04/5.3.1.BinaryExclusiveOr.ps1: -------------------------------------------------------------------------------- 1 | 6 -bxor 13 -------------------------------------------------------------------------------- /Chapter04/9.2.1.Comma.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2, 3, 4 -------------------------------------------------------------------------------- /Chapter05/7.1.1.CreateArray.ps1: -------------------------------------------------------------------------------- 1 | $myArray = @() -------------------------------------------------------------------------------- /Chapter05/8.3.1.AccessKey.ps1: -------------------------------------------------------------------------------- 1 | $hashtable["Key1"] -------------------------------------------------------------------------------- /Chapter05/8.4.2.KeysProperty.ps1: -------------------------------------------------------------------------------- 1 | $hashtable.Keys -------------------------------------------------------------------------------- /Chapter05/9.1.3.AddMethod.ps1: -------------------------------------------------------------------------------- 1 | $list.Add("David") -------------------------------------------------------------------------------- /Chapter08/3.2.6.Math_Abs.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Abs(-45748) -------------------------------------------------------------------------------- /Chapter10/6.1.1_Get-PSProvider.ps1: -------------------------------------------------------------------------------- 1 | Get-PSProvider -------------------------------------------------------------------------------- /Chapter14/1.4.9_Test-WSMan.ps1: -------------------------------------------------------------------------------- 1 | Test-WSMan -UseSSL -------------------------------------------------------------------------------- /Chapter21/3.4.1_throw.ps1: -------------------------------------------------------------------------------- 1 | throw 'Error message' -------------------------------------------------------------------------------- /Chapter01/5.4.2_Get-Alias_ByName.ps1: -------------------------------------------------------------------------------- 1 | Get-Alias dir -------------------------------------------------------------------------------- /Chapter01/7.1.1_Providers.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_Providers -------------------------------------------------------------------------------- /Chapter03/2.3.5.Method_close.ps1: -------------------------------------------------------------------------------- 1 | $tcpClient.Close() -------------------------------------------------------------------------------- /Chapter04/1.2.1.Addition_Simple.ps1: -------------------------------------------------------------------------------- 1 | 2.71828 + 3.14159 -------------------------------------------------------------------------------- /Chapter04/1.4.2.Multiplication_Strings.ps1: -------------------------------------------------------------------------------- 1 | 'hello' * 3 -------------------------------------------------------------------------------- /Chapter04/2.1.3.AddAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $i = 1 2 | $i += 20 -------------------------------------------------------------------------------- /Chapter04/8.4.1.RedirectToNull.ps1: -------------------------------------------------------------------------------- 1 | Get-Process > $null -------------------------------------------------------------------------------- /Chapter05/1.1.6.AssignmentMultiple.ps1: -------------------------------------------------------------------------------- 1 | $i = $j = 0 -------------------------------------------------------------------------------- /Chapter05/6.1.1.ValueTypeAssignment.ps1: -------------------------------------------------------------------------------- 1 | $i = $j = 5 -------------------------------------------------------------------------------- /Chapter05/8.4.3.ValuesProperty.ps1: -------------------------------------------------------------------------------- 1 | $hashtable.Values -------------------------------------------------------------------------------- /Chapter05/9.2.11.KeysProperty.ps1: -------------------------------------------------------------------------------- 1 | $dictionary.Keys -------------------------------------------------------------------------------- /Chapter05/9.2.12.ValuesProperty.ps1: -------------------------------------------------------------------------------- 1 | $dictionary.Values -------------------------------------------------------------------------------- /Chapter05/9.3.2.EnumerateQueue.ps1: -------------------------------------------------------------------------------- 1 | $queue.ToArray() -------------------------------------------------------------------------------- /Chapter05/9.4.2.EnumerateStack.ps1: -------------------------------------------------------------------------------- 1 | $stack.ToArray() -------------------------------------------------------------------------------- /Chapter10/6.1.3_Undo-Transaction.ps1: -------------------------------------------------------------------------------- 1 | Undo-Transaction -------------------------------------------------------------------------------- /Chapter11/4.4.3_WmiClass.ps1: -------------------------------------------------------------------------------- 1 | [WmiClass]"Win32_Process" -------------------------------------------------------------------------------- /Chapter11/5.1.13_AddToDACL.ps1: -------------------------------------------------------------------------------- 1 | $aclObject.DACL += $ace -------------------------------------------------------------------------------- /Chapter20/1.1.1_Ast.ps1: -------------------------------------------------------------------------------- 1 | { Write-Host 'content' }.Ast -------------------------------------------------------------------------------- /Chapter01/4.2.2_Get-Help_more.ps1: -------------------------------------------------------------------------------- 1 | Get-Help default | more -------------------------------------------------------------------------------- /Chapter01/4.2.5_Get-Help_Example.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Get-Variable -------------------------------------------------------------------------------- /Chapter02/4.1.1_Get-PSSnapIn.ps1: -------------------------------------------------------------------------------- 1 | Get-PSSnapIn -Registered -------------------------------------------------------------------------------- /Chapter04/1.1.3.OperatorPrecedence_Parens.ps1: -------------------------------------------------------------------------------- 1 | (3 + 2) * 2 -------------------------------------------------------------------------------- /Chapter04/1.2.2.Concatenation.ps1: -------------------------------------------------------------------------------- 1 | 'hello' + ' ' + 'world' -------------------------------------------------------------------------------- /Chapter04/1.2.8.JoinHashtable.ps1: -------------------------------------------------------------------------------- 1 | @{key1 = 1} + @{key2 = 2} -------------------------------------------------------------------------------- /Chapter04/2.1.1.Assignment.ps1: -------------------------------------------------------------------------------- 1 | $variable = 'some value' -------------------------------------------------------------------------------- /Chapter04/2.1.12.SubtractAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $i = 20 2 | $i -= 2 -------------------------------------------------------------------------------- /Chapter04/2.2.1.MultiplyAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $i = 2 2 | $i *= 2 -------------------------------------------------------------------------------- /Chapter04/9.2.2.SingleElementArray.ps1: -------------------------------------------------------------------------------- 1 | $array = ,1 2 | -------------------------------------------------------------------------------- /Chapter05/7.7.1.SetVariablesFromArray.ps1: -------------------------------------------------------------------------------- 1 | $i, $j = 1, 2 -------------------------------------------------------------------------------- /Chapter05/8.1.1.CreateEmptyHashtable.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{} -------------------------------------------------------------------------------- /Chapter05/9.2.5.StronglyTypedKey.ps1: -------------------------------------------------------------------------------- 1 | $dictionary.Add(1, 20) -------------------------------------------------------------------------------- /Chapter07/8.1.5.Accelerators_Get.ps1: -------------------------------------------------------------------------------- 1 | [Accelerators]::Get -------------------------------------------------------------------------------- /Chapter07/9.1.3.gacutil.ps1: -------------------------------------------------------------------------------- 1 | gacutil /l System.Windows.Forms -------------------------------------------------------------------------------- /Chapter08/5.3.2.Get-Date_StartOfDay.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).Date -------------------------------------------------------------------------------- /Chapter09/1.3.1.AnyCharacter.ps1: -------------------------------------------------------------------------------- 1 | 'abcdef' -match '......' -------------------------------------------------------------------------------- /Chapter09/1.5.1.EscapeCharacter.ps1: -------------------------------------------------------------------------------- 1 | '1 * 3' -match '\*' -------------------------------------------------------------------------------- /Chapter09/6.1.2.Repetition_Groups.ps1: -------------------------------------------------------------------------------- 1 | ([0-9]+\.){3}[0-9]+ -------------------------------------------------------------------------------- /Chapter09/6.1.3.Repetition_ZeroOrMore.ps1: -------------------------------------------------------------------------------- 1 | [0-9]+(\.[0-9]+)* -------------------------------------------------------------------------------- /Chapter12/4.4.3_Save.ps1: -------------------------------------------------------------------------------- 1 | $xDocument.Save("$pwd\test.xml") -------------------------------------------------------------------------------- /Chapter13/1.5.3_Certificate.ps1: -------------------------------------------------------------------------------- 1 | $certValidation.Certificate -------------------------------------------------------------------------------- /Chapter14/1.1.1_Disable-Remoting.ps1: -------------------------------------------------------------------------------- 1 | Disable-PSRemoting -------------------------------------------------------------------------------- /Chapter14/2.4.1_Enter-PSSession.ps1: -------------------------------------------------------------------------------- 1 | Enter-PSSession PSTest -------------------------------------------------------------------------------- /Chapter15/3.5.5.RunspacePool_GC_Collect.ps1: -------------------------------------------------------------------------------- 1 | [GC]::Collect() -------------------------------------------------------------------------------- /Chapter16/1.1.2_about_Requires.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_Requires -------------------------------------------------------------------------------- /Chapter18/1.2.2.enum_CastToInt.ps1: -------------------------------------------------------------------------------- 1 | [Int][MyEnum]::First -------------------------------------------------------------------------------- /Chapter21/3.6.1_BuiltInErrorVariable.ps1: -------------------------------------------------------------------------------- 1 | $Error.Clear() -------------------------------------------------------------------------------- /Chapter21/3.6.2_BuiltInErrorVariable_Index.ps1: -------------------------------------------------------------------------------- 1 | $Error[0] -------------------------------------------------------------------------------- /Chapter01/4.1.2_UpdatableHelp.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_Updatable_Help -------------------------------------------------------------------------------- /Chapter01/4.2.11_Get-Help_Full.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Get-Process –Full -------------------------------------------------------------------------------- /Chapter01/4.2.6_Get-Help_Online.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Get-Command -Online -------------------------------------------------------------------------------- /Chapter01/4.5.1_Get-Help_About.ps1: -------------------------------------------------------------------------------- 1 | Get-Help -Category HelpFile -------------------------------------------------------------------------------- /Chapter01/6.1.5_SwitchParam.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem -Recurse:$false -------------------------------------------------------------------------------- /Chapter01/6.1.6_CommonParams.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_CommonParameters -------------------------------------------------------------------------------- /Chapter01/6.4.11_WhatIfPreference.ps1: -------------------------------------------------------------------------------- 1 | $WhatIfPreference = $true -------------------------------------------------------------------------------- /Chapter01/6.4.2_Force.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Remove-Item -Parameter Force -------------------------------------------------------------------------------- /Chapter01/6.4.5_Clear-RecycleBin_Confirm.ps1: -------------------------------------------------------------------------------- 1 | Clear-RecycleBin -------------------------------------------------------------------------------- /Chapter02/1.3.1_PSMODULEPATH.ps1: -------------------------------------------------------------------------------- 1 | $env:PSModulePath -split ';' -------------------------------------------------------------------------------- /Chapter02/2.1.3_Get-Command.ps1: -------------------------------------------------------------------------------- 1 | Get-Command -Module PSWorkflow -------------------------------------------------------------------------------- /Chapter02/2.3.2_Find-Module_Filter.ps1: -------------------------------------------------------------------------------- 1 | Find-Module -Filter IIS -------------------------------------------------------------------------------- /Chapter02/4.2.1_Add-PSSnapIn.ps1: -------------------------------------------------------------------------------- 1 | Add-PSSnapIn WDeploySnapin3.0 -------------------------------------------------------------------------------- /Chapter03/2.2.2.Property_Parens.ps1: -------------------------------------------------------------------------------- 1 | (Get-Process -Id $PID).Name -------------------------------------------------------------------------------- /Chapter04/1.1.2.OperatorPrecedence_Shift.ps1: -------------------------------------------------------------------------------- 1 | 2 * 4 -shl 2 + 2 -------------------------------------------------------------------------------- /Chapter04/1.2.3.Concatenation_casting.ps1: -------------------------------------------------------------------------------- 1 | 'hello number ' + 1 -------------------------------------------------------------------------------- /Chapter04/2.1.9.AddToArray.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2 2 | $array += 3 -------------------------------------------------------------------------------- /Chapter04/3.1.2.CaseSensitiveEquals.ps1: -------------------------------------------------------------------------------- 1 | 'Trees' -ceq 'trees' -------------------------------------------------------------------------------- /Chapter04/7.1.1.As.ps1: -------------------------------------------------------------------------------- 1 | "1" -as [Int32] 2 | 'String' -as [Type] -------------------------------------------------------------------------------- /Chapter04/8.4.2.RedirectToNull_EmptyString.ps1: -------------------------------------------------------------------------------- 1 | Get-Process > '' -------------------------------------------------------------------------------- /Chapter04/9.1.1.Call.ps1: -------------------------------------------------------------------------------- 1 | $command = 'ipconfig' 2 | & $command -------------------------------------------------------------------------------- /Chapter04/9.3.1.Format.ps1: -------------------------------------------------------------------------------- 1 | '1: {0}, 2: {1}, 3: {2}' -f 1, 2, 3 -------------------------------------------------------------------------------- /Chapter04/9.3.4.Format_Currency.ps1: -------------------------------------------------------------------------------- 1 | 'The price is {0:C2}' -f 199 -------------------------------------------------------------------------------- /Chapter05/2.3.2.New-Variable_Equivalent.ps1: -------------------------------------------------------------------------------- 1 | $today = Get-Date -------------------------------------------------------------------------------- /Chapter05/3.1.1.VariableProvider.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem variable: -------------------------------------------------------------------------------- /Chapter05/7.4.2.IndexFromEnd.ps1: -------------------------------------------------------------------------------- 1 | $myArray[-1] 2 | $myArray[-2] -------------------------------------------------------------------------------- /Chapter05/7.4.4.ArrayOfIndexes.ps1: -------------------------------------------------------------------------------- 1 | $myArray[0..2 + 6..8 + -1] -------------------------------------------------------------------------------- /Chapter05/7.6.6.RemovingElements_ByIndex.ps1: -------------------------------------------------------------------------------- 1 | $oldArray = 1..100 -------------------------------------------------------------------------------- /Chapter08/3.2.8.Math_Sqrt.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Sqrt(9) # Returns 3 -------------------------------------------------------------------------------- /Chapter10/3.2.1_FileAttributes.ps1: -------------------------------------------------------------------------------- 1 | [System.IO.FileAttributes]3 -------------------------------------------------------------------------------- /Chapter10/5.4.5_Get-Acl_Error.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\Temp\ACL\3 -------------------------------------------------------------------------------- /Chapter10/6.2.4_Complete-Transaction.ps1: -------------------------------------------------------------------------------- 1 | Complete-Transaction -------------------------------------------------------------------------------- /Chapter14/1.3.2_Restart_winrm.ps1: -------------------------------------------------------------------------------- 1 | Restart-Service winrm 2 | -------------------------------------------------------------------------------- /Chapter21/3.1.1_ErrorRecord.ps1: -------------------------------------------------------------------------------- 1 | 100 / 0 2 | $record = $Error[0] -------------------------------------------------------------------------------- /Chapter21/3.2.1_Write-Error_Message.ps1: -------------------------------------------------------------------------------- 1 | Write-Error 'Message' -------------------------------------------------------------------------------- /Chapter21/3.5.1_throw_ExceptionType.ps1: -------------------------------------------------------------------------------- 1 | throw 'Error message' -------------------------------------------------------------------------------- /Chapter21/5.3.1_Connect-Server.ps1: -------------------------------------------------------------------------------- 1 | function Connect-Server {} -------------------------------------------------------------------------------- /Chapter01/4.2.10_Get-Help_Detailed.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Get-Process -Detailed -------------------------------------------------------------------------------- /Chapter01/4.2.3_Get-Help_ShowWindow.ps1: -------------------------------------------------------------------------------- 1 | Get-Help default -ShowWindow -------------------------------------------------------------------------------- /Chapter01/4.2.8_Get-Help_Examples.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Get-Process -Examples -------------------------------------------------------------------------------- /Chapter01/5.3.2_Get-Command_Wildcards.ps1: -------------------------------------------------------------------------------- 1 | Get-Command Get-*Firewall* -------------------------------------------------------------------------------- /Chapter01/5.5.3_AliasUse.ps1: -------------------------------------------------------------------------------- 1 | cd $env:TEMP 2 | Set-Location $env:TEMP -------------------------------------------------------------------------------- /Chapter01/5.5.4_New-Alias.ps1: -------------------------------------------------------------------------------- 1 | New-Alias grep -Value Select-String -------------------------------------------------------------------------------- /Chapter01/6.4.8_ConfirmPreference_Set.ps1: -------------------------------------------------------------------------------- 1 | $ConfirmPreference = 'Low' -------------------------------------------------------------------------------- /Chapter01/7.1.4_ProviderHelpCategory.ps1: -------------------------------------------------------------------------------- 1 | Get-Help -Category Provider -------------------------------------------------------------------------------- /Chapter01/7.2.4_Get-ChildItem_HKU.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem HKU: 2 | 3 | -------------------------------------------------------------------------------- /Chapter02/1.3.3_PSMODULEPATH_PSCore.ps1: -------------------------------------------------------------------------------- 1 | $env:PSModulePath -split ';' -------------------------------------------------------------------------------- /Chapter03/7.1.1.Export-Csv.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Export-Csv processes.csv -------------------------------------------------------------------------------- /Chapter04/2.1.10.AddArrayToArray.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2 2 | $array += 3, 4 -------------------------------------------------------------------------------- /Chapter04/2.1.4.AddAndAssign_Equivalent.ps1: -------------------------------------------------------------------------------- 1 | $i = 1 2 | $i = $i + 20 -------------------------------------------------------------------------------- /Chapter04/2.2.5.DivideAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $variable = 2 2 | $variable /= 2 -------------------------------------------------------------------------------- /Chapter04/9.3.2.Format_Percentage.ps1: -------------------------------------------------------------------------------- 1 | 'The pass mark is {0:P}' -f 0.8 -------------------------------------------------------------------------------- /Chapter04/9.3.3.Format_Hex.ps1: -------------------------------------------------------------------------------- 1 | '244 in Hexadecimal is {0:X2}' -f 244 -------------------------------------------------------------------------------- /Chapter05/4.2.1.Scope_Private.ps1: -------------------------------------------------------------------------------- 1 | $private:thisValue = "Some value" -------------------------------------------------------------------------------- /Chapter05/7.4.3.IndexRange.ps1: -------------------------------------------------------------------------------- 1 | $myArray[2..4] 2 | $myArray[-1..-5] -------------------------------------------------------------------------------- /Chapter05/7.7.4.SetVariablesFromArray_Underrun.ps1: -------------------------------------------------------------------------------- 1 | $i, $j, $k = 1, 2 -------------------------------------------------------------------------------- /Chapter05/8.3.2.AccessKey_Alternative.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{1 = 'one'} -------------------------------------------------------------------------------- /Chapter07/8.1.1.TypeAccelerators_PowerShell.ps1: -------------------------------------------------------------------------------- 1 | [PowerShell].Assembly -------------------------------------------------------------------------------- /Chapter07/9.1.1.UsingAssembly.ps1: -------------------------------------------------------------------------------- 1 | using assembly System.Windows.Forms -------------------------------------------------------------------------------- /Chapter08/3.2.3.Math_RoundMethod.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Round(2.123456789, 2) -------------------------------------------------------------------------------- /Chapter08/3.2.7.Math_Pow.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Pow(2, 8) # Returns 256 (28) -------------------------------------------------------------------------------- /Chapter09/1.5.4.ValidButInvalid.ps1: -------------------------------------------------------------------------------- 1 | 'domain\user' -match 'domain\.+' -------------------------------------------------------------------------------- /Chapter09/1.5.5.LiteralPeriod.ps1: -------------------------------------------------------------------------------- 1 | 'domain.user' -match 'domain\.+' -------------------------------------------------------------------------------- /Chapter09/6.1.1.Repetition_Start.ps1: -------------------------------------------------------------------------------- 1 | [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ -------------------------------------------------------------------------------- /Chapter10/1.2.2_Get-Item_Error.ps1: -------------------------------------------------------------------------------- 1 | Get-Item $env:USERPROFILE\AppData -------------------------------------------------------------------------------- /Chapter12/1.4.1_System.Web.ps1: -------------------------------------------------------------------------------- 1 | Add-Type -AssemblyName System.Web 2 | -------------------------------------------------------------------------------- /Chapter12/4.1.1_Add-Type.ps1: -------------------------------------------------------------------------------- 1 | Add-Type -AssemblyName System.Xml.Linq -------------------------------------------------------------------------------- /Chapter12/4.1.2_using_assembly.ps1: -------------------------------------------------------------------------------- 1 | using assembly System.Xml.Linq -------------------------------------------------------------------------------- /Chapter13/1.5.5_ChainStatus.ps1: -------------------------------------------------------------------------------- 1 | $certValidation.Chain.ChainStatus -------------------------------------------------------------------------------- /Chapter14/1.4.4_Set-WSManQuickConfig.ps1: -------------------------------------------------------------------------------- 1 | Set-WSManQuickConfig -UseSSL -------------------------------------------------------------------------------- /Chapter15/3.7.2.SetMaxRunspaces.ps1: -------------------------------------------------------------------------------- 1 | $runspacePool.SetMaxRunspaces(5) -------------------------------------------------------------------------------- /Chapter18/1.4.2.enum_cast_to_Boolean.ps1: -------------------------------------------------------------------------------- 1 | [Boolean][Ensure]::Absent -------------------------------------------------------------------------------- /Chapter01/4.2.4_Get-Help_All.ps1: -------------------------------------------------------------------------------- 1 | Get-Help * 2 | Get-Help -Category All -------------------------------------------------------------------------------- /Chapter01/4.2.9_Get-Help_Parameter.ps1: -------------------------------------------------------------------------------- 1 | Get-Help Import-Csv -Parameter Path -------------------------------------------------------------------------------- /Chapter01/4.3.2_Save-Help_All.ps1: -------------------------------------------------------------------------------- 1 | Save-Help -DestinationPath C:\PSHelp -------------------------------------------------------------------------------- /Chapter01/4.4.3_Update-Help_ImportHelp.ps1: -------------------------------------------------------------------------------- 1 | Update-Help -SourcePath C:\temp -------------------------------------------------------------------------------- /Chapter01/8.4.1_ExampleCommand.ps1: -------------------------------------------------------------------------------- 1 | Rename-Item oldname.txt newname.txt -------------------------------------------------------------------------------- /Chapter02/1.2.2_Get-Module_ListAvailable.ps1: -------------------------------------------------------------------------------- 1 | Get-Module -ListAvailable -------------------------------------------------------------------------------- /Chapter02/2.6.1_Save-Module.ps1: -------------------------------------------------------------------------------- 1 | Save-Module -Name Carbon -Path C:\Modules -------------------------------------------------------------------------------- /Chapter03/7.3.2.Import-CliXml.ps1: -------------------------------------------------------------------------------- 1 | $object = Import-Clixml .\object.xml -------------------------------------------------------------------------------- /Chapter04/2.1.8.CastToString.ps1: -------------------------------------------------------------------------------- 1 | [String]$string = 1 2 | $string += 'one' -------------------------------------------------------------------------------- /Chapter04/2.2.6.RemainderAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $variable = 10 2 | $variable %= 3 -------------------------------------------------------------------------------- /Chapter04/3.1.3.ExplicitCaseInsensitiveEquals.ps1: -------------------------------------------------------------------------------- 1 | 'Trees' -ieq 'trees' -------------------------------------------------------------------------------- /Chapter04/9.1.3.CallCommandAsString.ps1: -------------------------------------------------------------------------------- 1 | & 'ipconfig' '/displaydns' 2 | -------------------------------------------------------------------------------- /Chapter05/7.2.1.ArrayOfType.ps1: -------------------------------------------------------------------------------- 1 | [Int32[]]$myNumbers = 1, 2, $null, 3.45 -------------------------------------------------------------------------------- /Chapter05/7.3.1.AddElement.ps1: -------------------------------------------------------------------------------- 1 | $myArray = @() 2 | $myArray += "New value" -------------------------------------------------------------------------------- /Chapter05/7.4.6.ArrayOfIndexes_FirstMustBeArray_Cause.ps1: -------------------------------------------------------------------------------- 1 | 0..2 + 6..8 + -1 -------------------------------------------------------------------------------- /Chapter05/7.5.1.Updating.ps1: -------------------------------------------------------------------------------- 1 | $myArray = 1, 2, 9, 4, 5 2 | $myArray[2] = 3 -------------------------------------------------------------------------------- /Chapter05/7.6.10.RemovingElements_ByValue_Start.ps1: -------------------------------------------------------------------------------- 1 | $oldArray = 1..100 -------------------------------------------------------------------------------- /Chapter05/7.6.14.Clear.ps1: -------------------------------------------------------------------------------- 1 | $newArray = 1, 2, 3, 4, 5 2 | $newArray.Clear() -------------------------------------------------------------------------------- /Chapter05/8.3.3.AccessKey_IntegerKey.ps1: -------------------------------------------------------------------------------- 1 | $hashtable.1 2 | $hashtable[1] -------------------------------------------------------------------------------- /Chapter05/9.3.5.DequeueMethod.ps1: -------------------------------------------------------------------------------- 1 | $queue.Dequeue() # This returns Tom. -------------------------------------------------------------------------------- /Chapter07/6.2.2.StaticPropertyInvocation.ps1: -------------------------------------------------------------------------------- 1 | [System.Text.Encoding]::ASCII -------------------------------------------------------------------------------- /Chapter07/6.2.4.StaticFields.ps1: -------------------------------------------------------------------------------- 1 | [Int32]::MaxValue 2 | [Int32]::MinValue -------------------------------------------------------------------------------- /Chapter07/9.1.2.Add-Type.ps1: -------------------------------------------------------------------------------- 1 | Add-Type -AssemblyName System.Windows.Forms -------------------------------------------------------------------------------- /Chapter08/1.1.3.StringSplitMethodOnArray.ps1: -------------------------------------------------------------------------------- 1 | ('a,b', 'c,d').Split(',') -------------------------------------------------------------------------------- /Chapter08/5.3.5.Get-Date_ToUniversalTime.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).ToUniversalTime() -------------------------------------------------------------------------------- /Chapter09/1.3.2.AnyCharacter_NoAnchors.ps1: -------------------------------------------------------------------------------- 1 | 'abcdefghijkl' -match '......' -------------------------------------------------------------------------------- /Chapter10/2.1.1_Test-Path.ps1: -------------------------------------------------------------------------------- 1 | Test-Path HKLM:\Software\Publisher 2 | -------------------------------------------------------------------------------- /Chapter11/4.4.1_Get-WmiObject_Class.ps1: -------------------------------------------------------------------------------- 1 | Get-WmiObject Win32_Process -List -------------------------------------------------------------------------------- /Chapter12/3.10.2_RemoveAttribute.ps1: -------------------------------------------------------------------------------- 1 | $xml.list.RemoveAttribute('type') -------------------------------------------------------------------------------- /Chapter14/3.1.2_chsh.ps1: -------------------------------------------------------------------------------- 1 | # Run on Linux 2 | 3 | chsh -s /usr/bin/pwsh -------------------------------------------------------------------------------- /Chapter16/3.5.2_about_CommonParameters.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_commonparameters -------------------------------------------------------------------------------- /Chapter18/1.5.2.enum_flags_casting_string.ps1: -------------------------------------------------------------------------------- 1 | [Int][MyEnum]'First,Second' -------------------------------------------------------------------------------- /Chapter20/2.8.1_Mock.ps1: -------------------------------------------------------------------------------- 1 | Describe Subject { 2 | Mock Get-Date 3 | } -------------------------------------------------------------------------------- /Chapter01/5.3.1_Get-Command.ps1: -------------------------------------------------------------------------------- 1 | Get-Command Get-*Firewall* -Module NetSecurity -------------------------------------------------------------------------------- /Chapter01/5.5.2_Get-Alias_ByDefinition.ps1: -------------------------------------------------------------------------------- 1 | Get-Alias -Definition Get-ChildItem -------------------------------------------------------------------------------- /Chapter01/6.3.1_ParameterSets.ps1: -------------------------------------------------------------------------------- 1 | Stop-Process -Id $PID 2 | Stop-Process $PID -------------------------------------------------------------------------------- /Chapter01/6.4.1_PreferenceVariables.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_Preference_Variables -------------------------------------------------------------------------------- /Chapter01/6.4.4_Confirm_False.ps1: -------------------------------------------------------------------------------- 1 | Remove-Item .\IMadeThisUp.txt -Confirm:$false -------------------------------------------------------------------------------- /Chapter02/2.4.4_Install-Module_Reinstall.ps1: -------------------------------------------------------------------------------- 1 | Install-Module posh-git -Force -------------------------------------------------------------------------------- /Chapter02/4.2.3_Get-Command_SnapIn.ps1: -------------------------------------------------------------------------------- 1 | Get-Command -Module WDeploySnapin3.0 -------------------------------------------------------------------------------- /Chapter03/2.2.1.Property.ps1: -------------------------------------------------------------------------------- 1 | $process = Get-Process -Id $PID 2 | $process.Name -------------------------------------------------------------------------------- /Chapter03/4.2.3.Sort-Object_Property.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Sort-Object -Property Id -------------------------------------------------------------------------------- /Chapter03/7.2.1.Import-Csv.ps1: -------------------------------------------------------------------------------- 1 | Import-Csv TabDelimitedFile.tsv -Delimiter `t -------------------------------------------------------------------------------- /Chapter04/2.1.5.ConcatenateAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $string = 'one' 2 | $string += 'one' -------------------------------------------------------------------------------- /Chapter04/2.2.2.MultiplyStringAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $string = 'one' 2 | $string *= 2 -------------------------------------------------------------------------------- /Chapter04/2.2.4.MultiplyArrayAndAssign.ps1: -------------------------------------------------------------------------------- 1 | $variable = 1, 2 2 | $variable *= 2 -------------------------------------------------------------------------------- /Chapter05/1.1.2.VariableWithCurlyBraces.ps1: -------------------------------------------------------------------------------- 1 | ${My Variable} 2 | ${My-Variable} -------------------------------------------------------------------------------- /Chapter05/2.2.1.Get-Variable.ps1: -------------------------------------------------------------------------------- 1 | Get-Variable | Select-Object Name, Description -------------------------------------------------------------------------------- /Chapter05/2.3.1.New-Variable.ps1: -------------------------------------------------------------------------------- 1 | New-Variable -Name today -Value (Get-Date) -------------------------------------------------------------------------------- /Chapter05/7.3.2.AddElement_Equivalent.ps1: -------------------------------------------------------------------------------- 1 | $myArray = $myArray + "New value" -------------------------------------------------------------------------------- /Chapter05/7.7.3.SetVariablesFromArray_Overflow.ps1: -------------------------------------------------------------------------------- 1 | $i, $j, $k = 1, 2, 3, 4, 5 -------------------------------------------------------------------------------- /Chapter05/9.1.10.RemoveAll.ps1: -------------------------------------------------------------------------------- 1 | $list.RemoveAll( { $args[0] -eq "David" } ) 2 | -------------------------------------------------------------------------------- /Chapter05/9.2.2.AddMethod.ps1: -------------------------------------------------------------------------------- 1 | $dictionary.Add("Computer1", "192.168.10.222") -------------------------------------------------------------------------------- /Chapter05/9.2.4.PropertyNotation.ps1: -------------------------------------------------------------------------------- 1 | $dictionary.Computer3 = "192.168.10.134" -------------------------------------------------------------------------------- /Chapter05/9.4.5.PopMethod.ps1: -------------------------------------------------------------------------------- 1 | $stack.Pop() # This returns Under the bridge -------------------------------------------------------------------------------- /Chapter08/1.1.2.StringTrimMethodOnArray.ps1: -------------------------------------------------------------------------------- 1 | ('azzz', 'bzzz', 'czzz').Trim('z') -------------------------------------------------------------------------------- /Chapter10/1.2.3_Get-Item_Force.ps1: -------------------------------------------------------------------------------- 1 | Get-Item $env:USERPROFILE\AppData -Force -------------------------------------------------------------------------------- /Chapter10/5.2.1_Get-Acl_Owner.ps1: -------------------------------------------------------------------------------- 1 | Get-Acl C:\Temp\ACL\1 | Select-Object Owner -------------------------------------------------------------------------------- /Chapter11/4.2.1_Get-WmiObject.ps1: -------------------------------------------------------------------------------- 1 | Get-WmiObject -Class Win32_ComputerSystem -------------------------------------------------------------------------------- /Chapter11/4.4.2_Get-WmiObject_Class_Recurse.ps1: -------------------------------------------------------------------------------- 1 | Get-WmiObject -List -Recurse -------------------------------------------------------------------------------- /Chapter14/5.1.3_Enable-WSManCredSSP_Server.ps1: -------------------------------------------------------------------------------- 1 | Enable-WSManCredSSP -Role Server -------------------------------------------------------------------------------- /Chapter14/6.1.1_CimSessionCommands.ps1: -------------------------------------------------------------------------------- 1 | Get-Command -ParameterName CimSession -------------------------------------------------------------------------------- /Chapter15/2.3.2.Unregister-Event.ps1: -------------------------------------------------------------------------------- 1 | Get-EventSubscriber | Unregister-Event -------------------------------------------------------------------------------- /Chapter15/3.1.1.PowerShellInstance.ps1: -------------------------------------------------------------------------------- 1 | $psInstance = [PowerShell]::Create() -------------------------------------------------------------------------------- /Chapter16/1.4.1_about_comment_based_help.ps1: -------------------------------------------------------------------------------- 1 | Get-Help about_Comment_Based_Help -------------------------------------------------------------------------------- /Chapter16/2.5.8_Cast_to_Void_Parentheses.ps1: -------------------------------------------------------------------------------- 1 | [Void](Get-Command Get-Command) -------------------------------------------------------------------------------- /Chapter18/1.3.1.enum_no_value.ps1: -------------------------------------------------------------------------------- 1 | enum MyEnum { 2 | Zero 3 | One 4 | } -------------------------------------------------------------------------------- /Chapter20/2.7.2_TestDrive_AfterAll.ps1: -------------------------------------------------------------------------------- 1 | AfterAll { 2 | Pop-Location 3 | } -------------------------------------------------------------------------------- /Chapter01/4.4.2_Update-Help_SingleModule.ps1: -------------------------------------------------------------------------------- 1 | Update-Help -Module DnsClient -Verbose -------------------------------------------------------------------------------- /Chapter01/6.2.1_ParameterValues.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -ClassName Win32_OperatingSystem -------------------------------------------------------------------------------- /Chapter01/6.4.6_Clear-RecycleBin_Confirm_False.ps1: -------------------------------------------------------------------------------- 1 | Clear-RecycleBin -Confirm:$false -------------------------------------------------------------------------------- /Chapter01/7.1.3_ProviderHelp.ps1: -------------------------------------------------------------------------------- 1 | Get-Help -Name Certificate -Category Provider 2 | -------------------------------------------------------------------------------- /Chapter02/1.3.4_Import-Module_PSCore.ps1: -------------------------------------------------------------------------------- 1 | Import-Module NetSecurity -SkipEditionCheck -------------------------------------------------------------------------------- /Chapter02/2.4.2_Install-Module_Scope.ps1: -------------------------------------------------------------------------------- 1 | Install-Module carbon -Scope CurrentUser -------------------------------------------------------------------------------- /Chapter03/2.3.8.Method_chaining.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).Date.AddDays(-1).ToString('u') 2 | -------------------------------------------------------------------------------- /Chapter03/3.2.2.ForEach-Object_MemberExpansion.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | ForEach-Object Path -------------------------------------------------------------------------------- /Chapter03/4.1.5.Select-Object_Last.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\ | Select-Object -Last 3 -------------------------------------------------------------------------------- /Chapter04/1.7.2.RightShift.ps1: -------------------------------------------------------------------------------- 1 | 156 -shr 1 2 | 3 | # Expects output: 4 | # 5 | # 78 -------------------------------------------------------------------------------- /Chapter04/1.7.3.shl_OutOfRange.ps1: -------------------------------------------------------------------------------- 1 | 3 -shr 1 2 | 3 | # Expects output: 4 | # 5 | # 1 -------------------------------------------------------------------------------- /Chapter04/1.7.7.shl_IPAddress.ps1: -------------------------------------------------------------------------------- 1 | (192 -shl 24) + (168 -shl 16) + (4 -shl 8) + 32 -------------------------------------------------------------------------------- /Chapter05/2.5.2.Set-Variable_Private.ps1: -------------------------------------------------------------------------------- 1 | Set-Variable objectCount -Option Private -------------------------------------------------------------------------------- /Chapter05/3.1.2.VariableProvider_Test-Path.ps1: -------------------------------------------------------------------------------- 1 | Test-Path variable:\VerbosePreference -------------------------------------------------------------------------------- /Chapter05/3.1.3.VariableProvider_Set-Item.ps1: -------------------------------------------------------------------------------- 1 | Set-Item variable:\new -Value variable -------------------------------------------------------------------------------- /Chapter05/7.6.12.RemovingElements_ByValue_IndexOf.ps1: -------------------------------------------------------------------------------- 1 | $index = $oldArray.IndexOf(50) -------------------------------------------------------------------------------- /Chapter05/8.0.1.CommandsUsingHashtables.ps1: -------------------------------------------------------------------------------- 1 | Get-Command -ParameterType Hashtable -------------------------------------------------------------------------------- /Chapter05/8.2.1.AddMethod.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{} 2 | $hashtable.Add("Key1", "Value1") -------------------------------------------------------------------------------- /Chapter05/9.1.1.List.ps1: -------------------------------------------------------------------------------- 1 | $list = New-Object System.Collections.Generic.List[String] -------------------------------------------------------------------------------- /Chapter05/9.1.2.ArrayList.ps1: -------------------------------------------------------------------------------- 1 | $arrayList = New-object System.Collections.ArrayList -------------------------------------------------------------------------------- /Chapter05/9.1.6.FindIndex.ps1: -------------------------------------------------------------------------------- 1 | $index = $list.FindIndex( { $args[0] -eq 'Richard' } ) -------------------------------------------------------------------------------- /Chapter06/1.1.4.BooleanCastFalse.ps1: -------------------------------------------------------------------------------- 1 | [Boolean]0 2 | [Boolean]"" 3 | [Boolean]$null -------------------------------------------------------------------------------- /Chapter07/1.1.1.GetAssemblies.ps1: -------------------------------------------------------------------------------- 1 | [System.AppDomain]::CurrentDomain.GetAssemblies() -------------------------------------------------------------------------------- /Chapter07/5.2.1.newMethod.ps1: -------------------------------------------------------------------------------- 1 | $stringBuilder = [System.Text.StringBuilder]::new() -------------------------------------------------------------------------------- /Chapter07/5.2.2.New-Object.ps1: -------------------------------------------------------------------------------- 1 | $stringBuilder = New-Object System.Text.StringBuilder -------------------------------------------------------------------------------- /Chapter07/9.1.7.UsingAssemblyPath.ps1: -------------------------------------------------------------------------------- 1 | using assembly 'C:\SomeDir\someAssembly.dll' -------------------------------------------------------------------------------- /Chapter07/9.2.1.UsingNamespace.ps1: -------------------------------------------------------------------------------- 1 | using namespace System.Net.NetworkInformation -------------------------------------------------------------------------------- /Chapter08/1.3.1.Split.ps1: -------------------------------------------------------------------------------- 1 | $myString = 'Surname,GivenName' 2 | $myString.Split(',') -------------------------------------------------------------------------------- /Chapter08/5.3.1.Get-Date_And_New-TImeSpan.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date) + (New-Timespan -Hours 6) -------------------------------------------------------------------------------- /Chapter08/5.4.3.Get-Date_StoringDates.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).ToUniversalTime().ToString('u') -------------------------------------------------------------------------------- /Chapter10/5.3.1_Get-Acl_Audit.ps1: -------------------------------------------------------------------------------- 1 | Get-Acl C:\Temp\ACL\1 -Audit | Format-List 2 | -------------------------------------------------------------------------------- /Chapter11/4.2.2_WmiSearcher.ps1: -------------------------------------------------------------------------------- 1 | ([WmiSearcher]"SELECT * FROM Win32_Process").Get() -------------------------------------------------------------------------------- /Chapter12/4.5.1_XNamespace.ps1: -------------------------------------------------------------------------------- 1 | [XNameSpace]'http://example/cars' + [XName]'engine' -------------------------------------------------------------------------------- /Chapter12/5.2.1_ConvertFrom-Json.ps1: -------------------------------------------------------------------------------- 1 | '{ "Property": "Value" }' | ConvertFrom-Json -------------------------------------------------------------------------------- /Chapter12/5.3.4_Remove-TypeData.ps1: -------------------------------------------------------------------------------- 1 | Get-TypeData System.DateTime | Remove-TypeData -------------------------------------------------------------------------------- /Chapter13/3.3.2_CreateObject.ps1: -------------------------------------------------------------------------------- 1 | $searchCondition = [SOAP.SearchCondition]::new() -------------------------------------------------------------------------------- /Chapter14/1.5.4_ReadAndWrite.ps1: -------------------------------------------------------------------------------- 1 | $readAndWrite = -2147483648 -bor 1073741824 2 | -------------------------------------------------------------------------------- /Chapter14/1.7.1_GetTrustedHosts.ps1: -------------------------------------------------------------------------------- 1 | Get-Item WSMan:\localhost\Client\TrustedHosts -------------------------------------------------------------------------------- /Chapter14/4.1.1_InstallOpenSSH.ps1: -------------------------------------------------------------------------------- 1 | # Run on Windows 2 | 3 | choco install openssh -------------------------------------------------------------------------------- /Chapter14/5.1.4_Disable-WSManCredSSP_Client.ps1: -------------------------------------------------------------------------------- 1 | Disable-WSManCredSSP -Role Client -------------------------------------------------------------------------------- /Chapter14/5.1.5_Disable-WSManCredSSP_Server.ps1: -------------------------------------------------------------------------------- 1 | Disable-WSManCredSSP -Role Server -------------------------------------------------------------------------------- /Chapter15/3.5.1.CreateRunspacePool.ps1: -------------------------------------------------------------------------------- 1 | [RunspaceFactory]::CreateRunspacePool(1, 5) -------------------------------------------------------------------------------- /Chapter18/1.3.2.enum_sequencing.ps1: -------------------------------------------------------------------------------- 1 | enum MyEnum { 2 | Five = 5 3 | Six 4 | } -------------------------------------------------------------------------------- /Chapter18/2.1.1.class.ps1: -------------------------------------------------------------------------------- 1 | class MyClass { 2 | [String]$Property = 'Value' 3 | } -------------------------------------------------------------------------------- /Chapter18/2.7.2.static_invocation.ps1: -------------------------------------------------------------------------------- 1 | [MyClass]::Property 2 | [MyClass]::Method() -------------------------------------------------------------------------------- /Chapter01/4.3.1_Save-Help.ps1: -------------------------------------------------------------------------------- 1 | Save-Help -DestinationPath C:\PSHelp -Module DnsClient 2 | -------------------------------------------------------------------------------- /Chapter01/6.2.2_ParameterValues_Array.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Name powershell, explorer, smss -------------------------------------------------------------------------------- /Chapter01/7.2.2_CertProvider.ps1: -------------------------------------------------------------------------------- 1 | Set-Location Cert:\LocalMachine\Root 2 | Get-ChildItem -------------------------------------------------------------------------------- /Chapter01/7.3.1_UsingProviders_FileSystem.ps1: -------------------------------------------------------------------------------- 1 | Set-Location C: 2 | Get-ChildItem -File -------------------------------------------------------------------------------- /Chapter03/1.1.1.StdOut.ps1: -------------------------------------------------------------------------------- 1 | $stdout = Get-CimInstance -ClassName Win32_ComputerSystem 2 | -------------------------------------------------------------------------------- /Chapter03/4.1.1.Select-Object.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Select-Object -Property Name, Id 2 | -------------------------------------------------------------------------------- /Chapter03/4.1.6.Select-Object_Skip.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\ | Select-Object -Skip 2 -Last 1 -------------------------------------------------------------------------------- /Chapter03/4.1.8.Select-Object_Unique.ps1: -------------------------------------------------------------------------------- 1 | 1, 1, 1, 3, 5, 2, 2, 4 | Select-Object -Unique -------------------------------------------------------------------------------- /Chapter04/2.1.2.Assignment_Property.ps1: -------------------------------------------------------------------------------- 1 | $host.UI.RawUI.WindowTitle = 'PowerShell window' -------------------------------------------------------------------------------- /Chapter05/3.1.4.VariableProvider_Get-Content.ps1: -------------------------------------------------------------------------------- 1 | Get-COntent variable:\OutputEncoding -------------------------------------------------------------------------------- /Chapter05/8.3.4.AccessKey_IntegerKey_Failure.ps1: -------------------------------------------------------------------------------- 1 | $hashtable."1" 2 | $hashtable["1"] 3 | -------------------------------------------------------------------------------- /Chapter05/9.1.4.InsertMethod.ps1: -------------------------------------------------------------------------------- 1 | $list.Insert(0, "Sarah") 2 | $list.Insert(2, "Jane") -------------------------------------------------------------------------------- /Chapter05/9.3.1.Queue.ps1: -------------------------------------------------------------------------------- 1 | $queue = New-Object System.Collections.Generic.Queue[String] -------------------------------------------------------------------------------- /Chapter07/1.1.2.GetAssemblyForType.ps1: -------------------------------------------------------------------------------- 1 | [System.Management.Automation.PowerShell].Assembly -------------------------------------------------------------------------------- /Chapter07/9.2.2.UsingNamespace_Result.ps1: -------------------------------------------------------------------------------- 1 | [NetworkInterface]::GetAllNetworkInterfaces() -------------------------------------------------------------------------------- /Chapter08/1.6.2.Remove.ps1: -------------------------------------------------------------------------------- 1 | $string = 'This is is an example' 2 | $string.Remove(4, 3) -------------------------------------------------------------------------------- /Chapter08/3.2.2.Hexadecimal.ps1: -------------------------------------------------------------------------------- 1 | 0x5eb4 2 | 3 | # Expects output: 4 | # 5 | # 24244 6 | -------------------------------------------------------------------------------- /Chapter08/5.1.3.Test-DateTime_Get-Date.ps1: -------------------------------------------------------------------------------- 1 | Test-DateTime -Date (Get-Date "11/10/2000") -------------------------------------------------------------------------------- /Chapter08/5.3.7.Get-Date_UtcKind.ps1: -------------------------------------------------------------------------------- 1 | $UtcDate = [DateTime]::new((Get-Date).Ticks, 'Utc') -------------------------------------------------------------------------------- /Chapter09/1.6.1.OptionalCharacters.ps1: -------------------------------------------------------------------------------- 1 | 'There are 23 sites in the domain' -match 'sites?' -------------------------------------------------------------------------------- /Chapter09/2.1.0.StartOfString.ps1: -------------------------------------------------------------------------------- 1 | $env:PATH -split ';' | Where-Object { $_ -match '^C' } -------------------------------------------------------------------------------- /Chapter09/4.1.1.CharacterClasses.ps1: -------------------------------------------------------------------------------- 1 | 'get' -match 'g[aeiou]t' 2 | 'got' -match 'g[aeiou]' -------------------------------------------------------------------------------- /Chapter09/4.1.10.Range_LIteralHyphen_Escaped.ps1: -------------------------------------------------------------------------------- 1 | 'one-two_three,four' -split '[_\-,]' -------------------------------------------------------------------------------- /Chapter09/4.1.9.Range_LIteralHyphen_Alternative.ps1: -------------------------------------------------------------------------------- 1 | 'one-two_three,four' -split '[_,-]' -------------------------------------------------------------------------------- /Chapter09/4.3.1.ClassSubtraction_Start.ps1: -------------------------------------------------------------------------------- 1 | 'The lazy cat sat on the mat' -match '[aeiou]' -------------------------------------------------------------------------------- /Chapter10/3.1.2_Get-Item_SetProperty.ps1: -------------------------------------------------------------------------------- 1 | (Get-Item 'somefile.txt').IsReadOnly = $true -------------------------------------------------------------------------------- /Chapter10/3.2.2_FileAttribute_eq.ps1: -------------------------------------------------------------------------------- 1 | [System.IO.FileAttributes]'ReadOnly, Hidden' -eq 3 -------------------------------------------------------------------------------- /Chapter10/6.3.5_Get-Command_UseTransaction.ps1: -------------------------------------------------------------------------------- 1 | Get-Command -ParameterName UseTransaction -------------------------------------------------------------------------------- /Chapter11/3.2.2_Get-CimInstance_Namespace.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance __Namespace -Namespace root -------------------------------------------------------------------------------- /Chapter11/4.7.1_CreateInstance.ps1: -------------------------------------------------------------------------------- 1 | (Get-WmiObject Win32_Trustee -List).CreateInstance() -------------------------------------------------------------------------------- /Chapter12/4.2.2_Load.ps1: -------------------------------------------------------------------------------- 1 | $xDocument = [System.Xml.Linq.XDocument]::Load("$pwd\cars.xml") -------------------------------------------------------------------------------- /Chapter12/4.5.2_XNamespace_Shorthand.ps1: -------------------------------------------------------------------------------- 1 | [XNamespace]'http://example/cars' + 'engine' -------------------------------------------------------------------------------- /Chapter13/3.2.8_ExplicitEnumValue.ps1: -------------------------------------------------------------------------------- 1 | $service.GetElementsByGroup([SOAP.Group]::Nonmetal) -------------------------------------------------------------------------------- /Chapter17/2.3.4.AllowEmptyCollection_Demo.ps1: -------------------------------------------------------------------------------- 1 | Test-AllowEmptyCollection -Parameter1 @() -------------------------------------------------------------------------------- /Chapter18/2.2.1.properties.ps1: -------------------------------------------------------------------------------- 1 | class MyClass { 2 | [String]$Property = 'Value' 3 | } -------------------------------------------------------------------------------- /Chapter20/1.3.1_PSScriptAnalyzer.ps1: -------------------------------------------------------------------------------- 1 | Install-Module PSScriptAnalyzer -Scope CurrentUser -------------------------------------------------------------------------------- /Chapter20/2.10.1_SimpleObject.ps1: -------------------------------------------------------------------------------- 1 | [PSCustomObject]@{ 2 | Property = "Value" 3 | } -------------------------------------------------------------------------------- /Chapter20/2.9.6_Assert-MockCalled_Times_0.ps1: -------------------------------------------------------------------------------- 1 | Assert-MockCalled Get-CimInstance -Times 0 -------------------------------------------------------------------------------- /Chapter01/6.1.1_PositionalParams.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Name powershell 2 | Get-Process powershell -------------------------------------------------------------------------------- /Chapter01/6.1.2_MandatoryNamedParams.ps1: -------------------------------------------------------------------------------- 1 | Get-ADUser -Filter 'sAMAccountName -eq "SomeName"' -------------------------------------------------------------------------------- /Chapter01/6.1.7_Verbose.ps1: -------------------------------------------------------------------------------- 1 | Start-Process notepad -Verbose -PassThru | Stop-Process -Verbose -------------------------------------------------------------------------------- /Chapter01/6.4.7_ConfirmPreference.ps1: -------------------------------------------------------------------------------- 1 | $ConfirmPreference 2 | 3 | # Expects High by default -------------------------------------------------------------------------------- /Chapter01/7.2.3_New-PSDrive.ps1: -------------------------------------------------------------------------------- 1 | New-PSDrive HKU -PSProvider Registry -Root HKEY_USERS 2 | -------------------------------------------------------------------------------- /Chapter02/2.4.3_Install-Module_Scope_Force.ps1: -------------------------------------------------------------------------------- 1 | Install-Module carbon -Scope CurrentUser -Force -------------------------------------------------------------------------------- /Chapter03/2.2.4.Property_Sub-property_Parens.ps1: -------------------------------------------------------------------------------- 1 | (Get-Process -Id $PID).StartTime.DayOfWeek -------------------------------------------------------------------------------- /Chapter03/2.3.4.Method_results.ps1: -------------------------------------------------------------------------------- 1 | $tcpClient.Connected 2 | 3 | # Expects: 4 | # 5 | # True -------------------------------------------------------------------------------- /Chapter03/3.3.1.Where-Object.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Where-Object StartTime -gt (Get-Date 17:00:00) -------------------------------------------------------------------------------- /Chapter03/4.1.4.Select-Object_First.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\ -Recurse | Select-Object -First 2 -------------------------------------------------------------------------------- /Chapter03/6.1.1.Compare-Object.ps1: -------------------------------------------------------------------------------- 1 | Compare-Object -ReferenceObject 1, 2 -DifferenceObject 1, 2 -------------------------------------------------------------------------------- /Chapter04/1.7.5.shl_Int16.ps1: -------------------------------------------------------------------------------- 1 | ([Int16]255) -shl 1 2 | 3 | # Expects output: 4 | # 5 | # 510 -------------------------------------------------------------------------------- /Chapter04/1.7.6.shl_SIgning.ps1: -------------------------------------------------------------------------------- 1 | ([SByte]64) -shl 1 2 | 3 | # Expects output: 4 | # 5 | # -128 -------------------------------------------------------------------------------- /Chapter04/2.1.11.AddToHashtable.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{key1 = 1} 2 | $hashtable += @{key2 = 2} -------------------------------------------------------------------------------- /Chapter04/3.3.2.NotEqual_Examples.ps1: -------------------------------------------------------------------------------- 1 | 20 -ne 100 2 | 'this' -ne 'that' 3 | $false -ne 'false' -------------------------------------------------------------------------------- /Chapter04/3.6.2.In.ps1: -------------------------------------------------------------------------------- 1 | 1 -in 1, 2, 3 # Returns true 2 | 4 -in 1, 2, 3 # Returns false -------------------------------------------------------------------------------- /Chapter05/7.1.3.ArrayOfElements.ps1: -------------------------------------------------------------------------------- 1 | $myGreetings = "Hello world", "Hello sun", "Hello moon" -------------------------------------------------------------------------------- /Chapter05/7.1.6.ArrayOfElements_MixedType.ps1: -------------------------------------------------------------------------------- 1 | $myThings = "Hello world", 2, 34.23, (Get-Date) -------------------------------------------------------------------------------- /Chapter05/8.1.2.CreateHashtableWithKeys.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{Key1 = "Value1"; Key2 = "Value2"} -------------------------------------------------------------------------------- /Chapter05/9.4.1.Stack.ps1: -------------------------------------------------------------------------------- 1 | $stack = New-Object System.Collections.Generic.Stack[String] 2 | -------------------------------------------------------------------------------- /Chapter07/5.2.14.New-Object_Fix.ps1: -------------------------------------------------------------------------------- 1 | $memoryStream = New-Object System.IO.MemoryStream(,$bytes) -------------------------------------------------------------------------------- /Chapter08/1.11.1.MethodChaining.ps1: -------------------------------------------------------------------------------- 1 | ' ONe*? '.Trim().TrimEnd('?*').ToLower().Replace('o', 'O') -------------------------------------------------------------------------------- /Chapter08/1.3.7.SplitChoosesCharArray.ps1: -------------------------------------------------------------------------------- 1 | $string = 'one||two||three' 2 | $string.Split('||') -------------------------------------------------------------------------------- /Chapter08/2.2.4.ConvertFrom-Csv_FirstLineIsHeader.ps1: -------------------------------------------------------------------------------- 1 | 'Name,Age', 'David,28' | ConvertFrom-Csv -------------------------------------------------------------------------------- /Chapter09/1.4.1.Repetition_ZeroOrMore.ps1: -------------------------------------------------------------------------------- 1 | 'aaabc' -match 'a*'# Returns true, matches 'aaa' -------------------------------------------------------------------------------- /Chapter09/5.1.1.Alternation.ps1: -------------------------------------------------------------------------------- 1 | 'one', 'two', 'three' | Where-Object { $_ -match 'one|three' } -------------------------------------------------------------------------------- /Chapter10/5.8.1_RightsEnum.ps1: -------------------------------------------------------------------------------- 1 | [System.Security.AccessControl.FileSystemRights].GetEnumNames() -------------------------------------------------------------------------------- /Chapter12/1.1.1_ConvertTo-Html.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | ConvertTo-Html -Property Name, Id, WorkingSet -------------------------------------------------------------------------------- /Chapter12/3.2.3_SelectNodes.ps1: -------------------------------------------------------------------------------- 1 | ([Xml]$string).SelectNodes('//car[colour="Green"]/engine') -------------------------------------------------------------------------------- /Chapter14/1.3.1_Get-WSManInstance.ps1: -------------------------------------------------------------------------------- 1 | Set-Item WSMan:\localhost\MaxEnvelopeSizekb 1024 2 | -------------------------------------------------------------------------------- /Chapter14/2.6.2_Import-Module_ExportedSession.ps1: -------------------------------------------------------------------------------- 1 | Import-Module "NetAdapter-$computerName" -------------------------------------------------------------------------------- /Chapter16/3.2.3_Test-Nullable.ps1: -------------------------------------------------------------------------------- 1 | Test-Nullable -Date $null 2 | 3 | # Expected to not throw -------------------------------------------------------------------------------- /Chapter18/2.2.3.properties_accessing.ps1: -------------------------------------------------------------------------------- 1 | $instance = [MyClass]::new() 2 | $instance.Property -------------------------------------------------------------------------------- /Chapter01/6.4.10_BypassingWhatIf.ps1: -------------------------------------------------------------------------------- 1 | 'Some message' | Out-File $env:TEMP\test.txt -WhatIf:$false -------------------------------------------------------------------------------- /Chapter03/4.1.2.Select-Object_Wildcards.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Select-Object -Property Name, *Memory -------------------------------------------------------------------------------- /Chapter04/4.2.3.ReplaceWithTokens.ps1: -------------------------------------------------------------------------------- 1 | 'value1,value2,value3' -replace '(.*),(.*),(.*)', '$3,$2,$1' -------------------------------------------------------------------------------- /Chapter04/9.1.2.CallScriptBlock.ps1: -------------------------------------------------------------------------------- 1 | $scriptBlock = { Write-Host 'Hello world' } 2 | & $scriptBlock -------------------------------------------------------------------------------- /Chapter05/6.1.2.ValueTypeAssignment_Deviation.ps1: -------------------------------------------------------------------------------- 1 | $i = $j = 5 2 | $i++ 3 | $i += 1 4 | $i = $i + 1 -------------------------------------------------------------------------------- /Chapter05/8.4.1.Start.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ 2 | Key1 = 'Value1' 3 | Key2 = 'Value2' 4 | } -------------------------------------------------------------------------------- /Chapter09/1.4.2.Repetition_ZeroOrMore_Zero.ps1: -------------------------------------------------------------------------------- 1 | 'bcd' -match 'a*' # Returns true, matches nothing -------------------------------------------------------------------------------- /Chapter09/2.1.1.EndOfString.ps1: -------------------------------------------------------------------------------- 1 | $env:PATH -split ';' | Where-Object { $_ -match '\\.+\\.+\\.+$' } -------------------------------------------------------------------------------- /Chapter09/4.2.1.NegatedCharacterClass.ps1: -------------------------------------------------------------------------------- 1 | 'Ba%by8 a12315tthe1231 k#.,154eyboard' -replace '[^a-z ]' -------------------------------------------------------------------------------- /Chapter10/4.1.3_Remove-ItemProperty.ps1: -------------------------------------------------------------------------------- 1 | Remove-ItemProperty -Path HKCU:\Environment -Name NewValue -------------------------------------------------------------------------------- /Chapter11/2.1.4_WQL_Wildcards.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_Service -Filter 'PathName LIKE "_:\\%.exe"' -------------------------------------------------------------------------------- /Chapter11/2.2.3_Wmi_accelerator.ps1: -------------------------------------------------------------------------------- 1 | [Wmi]"Win32_Process.Handle=$PID" | Select-Object Name, Handle -------------------------------------------------------------------------------- /Chapter11/3.1.3_Get-CimInstance_Property.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_UserAccount -Property Name, SID -------------------------------------------------------------------------------- /Chapter14/1.5.1_Get-PSSessionConfiguration.ps1: -------------------------------------------------------------------------------- 1 | Get-PSSessionConfiguration Microsoft.PowerShell 2 | -------------------------------------------------------------------------------- /Chapter14/2.2.2_SessionsAreGlobal.ps1: -------------------------------------------------------------------------------- 1 | $session = New-PSSession -ComputerName PSTest -Name Example -------------------------------------------------------------------------------- /Chapter14/3.1.3_yum.ps1: -------------------------------------------------------------------------------- 1 | # Run on Linux 2 | 3 | yum install omi.x86_64 omi-psrp-server.x86_64 4 | -------------------------------------------------------------------------------- /Chapter18/1.4.1.enum_vs_ValidateSet.ps1: -------------------------------------------------------------------------------- 1 | enum Ensure { 2 | Absent 3 | Present 4 | } 5 | -------------------------------------------------------------------------------- /Chapter20/1.3.3_Invoke-ScriptAnalyzer.ps1: -------------------------------------------------------------------------------- 1 | Invoke-ScriptAnalyzer .\Show-Password.ps1 | Format-List -------------------------------------------------------------------------------- /Chapter20/2.10.16_New-CimInstance_WithoutNamespace.ps1: -------------------------------------------------------------------------------- 1 | New-CimInstance MSFT_NetAdapter -ClientOnly -------------------------------------------------------------------------------- /Chapter21/3.5.2_throw_ArgumentException.ps1: -------------------------------------------------------------------------------- 1 | throw [ArgumentException]::new('Unsupported value') -------------------------------------------------------------------------------- /Chapter01/7.4.1_UsingProviders_Certificate.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem Cert:\LocalMachine\Root -ExpiringInDays 730 -------------------------------------------------------------------------------- /Chapter02/2.1.4_Import-Module_MaximumVersion.ps1: -------------------------------------------------------------------------------- 1 | Import-Module PSScriptAnalyzer -MaxmimumVersion 1.7.0 -------------------------------------------------------------------------------- /Chapter02/2.4.1_Install-Module.ps1: -------------------------------------------------------------------------------- 1 | Find-Module posh-git | Install-Module 2 | Install-Module posh-git -------------------------------------------------------------------------------- /Chapter02/3.1.1_InstallWindowsCompatibility.ps1: -------------------------------------------------------------------------------- 1 | Install-Module WindowsCompatibility -Scope CurrentUser -------------------------------------------------------------------------------- /Chapter04/1.4.4.ArrayMultiplication.ps1: -------------------------------------------------------------------------------- 1 | @('one', 'two') * 2 2 | ('one', 'two') * 2 3 | 'one', 'two' * 2 -------------------------------------------------------------------------------- /Chapter04/1.7.4.shl_OutOfRange_Byte.ps1: -------------------------------------------------------------------------------- 1 | ([Byte]255) -shl 1 2 | 3 | # Expects output: 4 | # 5 | # 254 -------------------------------------------------------------------------------- /Chapter04/4.2.1.Replace.ps1: -------------------------------------------------------------------------------- 1 | 'abababab' -replace 'a', 'c' 2 | 3 | # Expects output: 4 | # 5 | # cbcbcbcb -------------------------------------------------------------------------------- /Chapter05/7.1.4.ArrayOfElements_Alternate.ps1: -------------------------------------------------------------------------------- 1 | $myGreetings = @("Hello world", "Hello sun", "Hello moon") -------------------------------------------------------------------------------- /Chapter05/7.4.1.GetByIndex.ps1: -------------------------------------------------------------------------------- 1 | $myArray = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 2 | $myArray[0] 3 | $myArray[1] -------------------------------------------------------------------------------- /Chapter05/7.6.4.RemovingElements_NullElements_Pipeline.ps1: -------------------------------------------------------------------------------- 1 | $myArray | ForEach-Object { Write-Host $_ } -------------------------------------------------------------------------------- /Chapter05/7.6.7.RemovingElements_ByIndex_NewArray.ps1: -------------------------------------------------------------------------------- 1 | $newArray = $oldArray[0..48] + $oldArray[50..99] -------------------------------------------------------------------------------- /Chapter05/8.5.3.ClearMethod.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{one = 1; two = 2; three = 3} 2 | $hashtable.Clear() 3 | -------------------------------------------------------------------------------- /Chapter06/2.1.1.foreach.ps1: -------------------------------------------------------------------------------- 1 | foreach ($process in Get-Process) { 2 | Write-Host $process.Name 3 | } -------------------------------------------------------------------------------- /Chapter07/2.1.1.GetAssemblies_ImplicitSystemNamespace.ps1: -------------------------------------------------------------------------------- 1 | [AppDomain]::CurrentDomain.GetAssemblies() -------------------------------------------------------------------------------- /Chapter07/2.1.2.GetAssemblies_ImplicitSystemNamespace_2.ps1: -------------------------------------------------------------------------------- 1 | [Management.Automation.PowerShell].Assembly -------------------------------------------------------------------------------- /Chapter08/1.4.1.Replace.ps1: -------------------------------------------------------------------------------- 1 | $string = 'This is the first example' 2 | $string.Replace('first', 'second') -------------------------------------------------------------------------------- /Chapter08/1.9.4.ToTitleCase_Alternative.ps1: -------------------------------------------------------------------------------- 1 | $host.CurrentCulture.TextInfo.ToTitleCase('another title') -------------------------------------------------------------------------------- /Chapter08/2.2.2.ConvertFrom-Csv.ps1: -------------------------------------------------------------------------------- 1 | "David,0123456789,28" | ConvertFrom-Csv -Header Name, Phone, Age -------------------------------------------------------------------------------- /Chapter08/4.0.2.Convert_ToInt32_FromBinary.ps1: -------------------------------------------------------------------------------- 1 | [Convert]::ToInt32('01000111110101', 2) # Returns 4597 -------------------------------------------------------------------------------- /Chapter08/4.0.3.Convert_ToInt32_FromHexadecimal.ps1: -------------------------------------------------------------------------------- 1 | [Convert]::ToInt32('FF9241', 16) # Returns 16749121 -------------------------------------------------------------------------------- /Chapter09/4.1.7.Range_VersionString.ps1: -------------------------------------------------------------------------------- 1 | 'version 0.1.2.3234' -match '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' -------------------------------------------------------------------------------- /Chapter10/3.2.3_FileAttribute_replace.ps1: -------------------------------------------------------------------------------- 1 | (Get-Item 'somefile.txt').Attributes = 'ReadOnly, Hidden' -------------------------------------------------------------------------------- /Chapter10/5.7.2_CopyAcl.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\4 2 | Set-Acl C:\Temp\ACL\5 -AclObject $acl -------------------------------------------------------------------------------- /Chapter11/2.1.5_Quoting_Outer_Single.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_Process -Filter 'Name="powershell.exe"' -------------------------------------------------------------------------------- /Chapter11/2.1.6_Quoting_Outer_Double.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_Process -Filter "Name='powershell.exe'" -------------------------------------------------------------------------------- /Chapter11/2.2.4_ASSOCIATORS_OF.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -Query "ASSOCIATORS OF {Win32_Process.Handle=$PID}" -------------------------------------------------------------------------------- /Chapter11/5.1.8_AceFlags.ps1: -------------------------------------------------------------------------------- 1 | [System.Security.AccessControl.AceFlags]0 2 | 3 | # Expects: 4 | # None -------------------------------------------------------------------------------- /Chapter11/5.3.2_SetFromSDDL.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\ 2 | $acl.SetSecurityDescriptorSddlForm($sddlString) -------------------------------------------------------------------------------- /Chapter12/3.7.3_SelectSingleNode_Set.ps1: -------------------------------------------------------------------------------- 1 | $xml.list.SelectSingleNode('./name[.="one"]').'#text' = 'three' -------------------------------------------------------------------------------- /Chapter13/1.9.9_OAUTH_token_header.ps1: -------------------------------------------------------------------------------- 1 | $headers = @{ 2 | Authorization = 'token {0}' -f $token 3 | } -------------------------------------------------------------------------------- /Chapter14/1.2.1_Get-WSManInstance.ps1: -------------------------------------------------------------------------------- 1 | Get-WSManInstance -ResourceUri wmicimv2/win32_operatingsystem 2 | -------------------------------------------------------------------------------- /Chapter14/4.1.3_GetPublicKey.ps1: -------------------------------------------------------------------------------- 1 | # Run on Windows 2 | 3 | Get-Content ~\.ssh\id_rsa.pub | Set-Clipboard -------------------------------------------------------------------------------- /Chapter14/5.1.2_Enable-WSManCredSSP.ps1: -------------------------------------------------------------------------------- 1 | Enable-WSManCredSSP -Role Client -DelegateComputer PSTest 2 | -------------------------------------------------------------------------------- /Chapter15/3.1.4.Equivalent.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Name powershell 2 | Get-Service | Select-Object -First 1 -------------------------------------------------------------------------------- /Chapter15/3.6.1.InitialSessionState.ps1: -------------------------------------------------------------------------------- 1 | $initialSessionState = [InitialSessionState]::CreateDefault2() -------------------------------------------------------------------------------- /Chapter16/1.2.1_Using_recap.ps1: -------------------------------------------------------------------------------- 1 | using assembly System.Xml.Linq 2 | using namespace System.Xml.Linq 3 | -------------------------------------------------------------------------------- /Chapter17/2.2.12.ValidatePattern_Options_CaseSensitive.ps1: -------------------------------------------------------------------------------- 1 | [ValidatePattern('^Hello', Options = 'None')] -------------------------------------------------------------------------------- /Chapter18/1.1.1.enum.ps1: -------------------------------------------------------------------------------- 1 | enum MyEnum { 2 | First = 1 3 | Second = 2 4 | Third = 3 5 | } -------------------------------------------------------------------------------- /Chapter20/2.9.7_Assert-MockCalled_Times_Exactly.ps1: -------------------------------------------------------------------------------- 1 | Assert-MockCalled Get-CimInstance -Times 1 -Exactly -------------------------------------------------------------------------------- /Chapter01/6.1.3_MandatoryPositionalParams.ps1: -------------------------------------------------------------------------------- 1 | Get-ADUser -Identity useridentity 2 | Get-ADUser useridentity -------------------------------------------------------------------------------- /Chapter01/6.4.12_ConfirmAndWhatIfPreference.ps1: -------------------------------------------------------------------------------- 1 | $WhatIfPreference = $true 2 | New-Item NewFile.txt -Confirm -------------------------------------------------------------------------------- /Chapter01/8.1.1_Splatting.ps1: -------------------------------------------------------------------------------- 1 | $getProcess = @{ 2 | Name = 'explorer' 3 | } 4 | Get-Process @getProcess -------------------------------------------------------------------------------- /Chapter02/2.3.1_Find-Module.ps1: -------------------------------------------------------------------------------- 1 | Find-Module Carbon 2 | Find-Module -Name Carbon 3 | Find-Module -Name Azure* -------------------------------------------------------------------------------- /Chapter03/2.1.1.Get-Member.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Id $PID | Get-Member 2 | 3 | # Expects full Get-Member output -------------------------------------------------------------------------------- /Chapter03/2.2.3.Property_Sub-property.ps1: -------------------------------------------------------------------------------- 1 | $process = Get-Process -Id $PID 2 | $process.StartTime.DayOfWeek -------------------------------------------------------------------------------- /Chapter03/2.4.4.AccessModifiers_Result.ps1: -------------------------------------------------------------------------------- 1 | Get-Item NewFile.txt | Select-Object -ExpandProperty CreationTime -------------------------------------------------------------------------------- /Chapter03/4.1.3.Select-Object_Exclude_Wildcards.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Select-Object -Property * -Exclude *Memory* -------------------------------------------------------------------------------- /Chapter03/7.1.4.Export-Csv_NoTypeInformation.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Export-Csv processes.csv -NoTypeInformation -------------------------------------------------------------------------------- /Chapter04/1.1.1.OperatorPrecedence.ps1: -------------------------------------------------------------------------------- 1 | 3 + 2 * 2 2 | 3 | # Expects 7 4 | 5 | 2 * 2 + 3 6 | 7 | # Expects 7 -------------------------------------------------------------------------------- /Chapter04/3.2.2.ArrayEqualsNull.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2 2 | if ($array -eq $null) { Write-Host 'Variable not set' } -------------------------------------------------------------------------------- /Chapter05/1.1.1.VariableNames.ps1: -------------------------------------------------------------------------------- 1 | $123 2 | $x 3 | $my_variable 4 | $variable 5 | $varIABle 6 | $Path_To_File -------------------------------------------------------------------------------- /Chapter05/2.4.1.Remove-Variable.ps1: -------------------------------------------------------------------------------- 1 | $psProcesses = Get-Process powershell 2 | Remove-Variable psProcesses -------------------------------------------------------------------------------- /Chapter05/2.5.1.Set-Variable.ps1: -------------------------------------------------------------------------------- 1 | Set-Variable objectCount -Description 'The number of objects in the queue' -------------------------------------------------------------------------------- /Chapter05/2.6.5.VariableProvider_Get-Item.ps1: -------------------------------------------------------------------------------- 1 | $new = 123 2 | Get-Item variable:\new 3 | Get-Item variable:new -------------------------------------------------------------------------------- /Chapter05/7.6.11.RemovingElements_ByValue_Where-Object.ps1: -------------------------------------------------------------------------------- 1 | $newArray = $oldArray | Where-Object { $_ -ne 50 } -------------------------------------------------------------------------------- /Chapter05/8.5.1.RemoveMethod.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ Existing = "Existing" } 2 | $hashtable.Remove("Existing") -------------------------------------------------------------------------------- /Chapter06/1.1.3.BooleanCastTrue.ps1: -------------------------------------------------------------------------------- 1 | [Boolean]1 2 | [Boolean]-1 3 | [Boolean]2016 4 | [Boolean]"Hello world -------------------------------------------------------------------------------- /Chapter06/1.1.5.IfAndAssign.ps1: -------------------------------------------------------------------------------- 1 | if ($i = 1) { 2 | Write-Host "Implicit true. The variable i is $i" 3 | } -------------------------------------------------------------------------------- /Chapter07/6.3.4.StaticMethodInvocation_Arguments.ps1: -------------------------------------------------------------------------------- 1 | [System.IO.Path]::ChangeExtension("C:\none.exe", "bak") -------------------------------------------------------------------------------- /Chapter08/1.10.1.Contains.ps1: -------------------------------------------------------------------------------- 1 | $string = 'I am the subject' 2 | $string.Contains('the') # Returns $true -------------------------------------------------------------------------------- /Chapter08/1.3.6.CharArray.ps1: -------------------------------------------------------------------------------- 1 | [char[]]$characters = [string[]]('a', 'b', 'c') 2 | [char[]]$characters = 'abc' -------------------------------------------------------------------------------- /Chapter08/1.9.1.ToUpperAndToLower.ps1: -------------------------------------------------------------------------------- 1 | 'aBc'.ToUpper() # Returns ABC 2 | 'AbC'.ToLower() # Returns abc -------------------------------------------------------------------------------- /Chapter08/3.1.1.ByteMultiplicationOperators.ps1: -------------------------------------------------------------------------------- 1 | 22.5GB 2 | 3 | # Expects output: 4 | # 5 | # 24159191040 -------------------------------------------------------------------------------- /Chapter08/3.2.9.Math_PiAndE.ps1: -------------------------------------------------------------------------------- 1 | [Math]::pi # π, 3.14159265358979 2 | [Math]::e # e, 2.71828182845905 -------------------------------------------------------------------------------- /Chapter08/5.5.3.ComparingDates_RightHandSideCasting_USFormat.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date "13/01/2017") -gt "01/12/2017" -------------------------------------------------------------------------------- /Chapter09/1.5.3.EscapingTheEscapeCharacter_WithReplace.ps1: -------------------------------------------------------------------------------- 1 | 'domain\user' -replace 'domain\\', 'newdomain\' -------------------------------------------------------------------------------- /Chapter09/4.3.3.ClassSubtraction_UsingSubtraction.ps1: -------------------------------------------------------------------------------- 1 | 'The lazy cat sat on the mat' -match '[a-z-[aeiou]]' -------------------------------------------------------------------------------- /Chapter10/3.2.8_FileAttributes_AsString_Explain.ps1: -------------------------------------------------------------------------------- 1 | [System.IO.FileAttributes]'ReadOnly, Hidden, ReadOnly' -------------------------------------------------------------------------------- /Chapter13/2.1.1_Invoke-RestMethod.ps1: -------------------------------------------------------------------------------- 1 | Invoke-RestMethod -Uri https://api.github.com/users/powershell/repos -------------------------------------------------------------------------------- /Chapter14/3.1.1_ListShells.ps1: -------------------------------------------------------------------------------- 1 | # Run on Linux 2 | 3 | Get-Content /etc/shells # Use cat or less in Bash -------------------------------------------------------------------------------- /Chapter15/2.4.3.Cleanup.ps1: -------------------------------------------------------------------------------- 1 | Get-EventSubscriber | Unregister-Event 2 | Remove-Item C:\Audit\DataActivity.log -------------------------------------------------------------------------------- /Chapter15/3.4.3.Waiting.ps1: -------------------------------------------------------------------------------- 1 | while ($jobs.State -eq 'Running') { 2 | Start-Sleep -Milliseconds 100 3 | } -------------------------------------------------------------------------------- /Chapter16/1.5.1_line_break_on_pipe.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | 2 | Where-Object Name -match 'po?w(er)?sh(ell)?' -------------------------------------------------------------------------------- /Chapter18/1.5.3.enum_flags_casting_int.ps1: -------------------------------------------------------------------------------- 1 | [MyEnum]6 2 | 3 | # Expects output: 4 | # 5 | # Second, Third -------------------------------------------------------------------------------- /Chapter19/1.2.2.Export-ModuleMember_wildcards.ps1: -------------------------------------------------------------------------------- 1 | Export-ModuleMember -Function *-ComputerDescription 2 | -------------------------------------------------------------------------------- /Chapter20/2.9.8_ParameterFilter.ps1: -------------------------------------------------------------------------------- 1 | Mock Test-Path { $true } -ParameterFilter { $Path -eq 'C:\Somewhere' } -------------------------------------------------------------------------------- /Chapter01/4.2.7_Get-Help_Get-Process.ps1: -------------------------------------------------------------------------------- 1 | Get-Process 2 | Get-Process powershell 3 | Get-Process -Name powershell -------------------------------------------------------------------------------- /Chapter01/8.4.2_PositionalSplatting.ps1: -------------------------------------------------------------------------------- 1 | $renameItem = 'oldname.txt', 'newname.txt' 2 | Rename-Item @renameItem -------------------------------------------------------------------------------- /Chapter04/4.2.2.ReplaceWithNothing.ps1: -------------------------------------------------------------------------------- 1 | 'abababab' -replace 'a' bbbb 2 | 3 | # Expects output: 4 | # 5 | # bbbb -------------------------------------------------------------------------------- /Chapter04/6.4.1.Not.ps1: -------------------------------------------------------------------------------- 1 | -not $false 2 | -not (Test-Path X:\) 3 | -not ($true -and $false) 4 | !($true -and $false) -------------------------------------------------------------------------------- /Chapter04/8.4.3.RedirectMultipleToNull.ps1: -------------------------------------------------------------------------------- 1 | .\somecommand.exe 2> $null 3> $null 2 | .\somecommand.exe *> $null 3 | -------------------------------------------------------------------------------- /Chapter04/9.4.1.PostIncrement.ps1: -------------------------------------------------------------------------------- 1 | for ($i = 0; $i -le 15; $i++) { 2 | Write-Host $i -ForegroundColor $i 3 | } -------------------------------------------------------------------------------- /Chapter04/9.4.2.PostDecrement.ps1: -------------------------------------------------------------------------------- 1 | for ($i = 15; $i -ge 0; $i--) { 2 | Write-Host $i -ForegroundColor $i 3 | } -------------------------------------------------------------------------------- /Chapter05/7.4.7.ArrayOfIndexes_FirstMustBeArray_Fix.ps1: -------------------------------------------------------------------------------- 1 | $myArray[@(0) + 6..8 + -1] 2 | $myArray[0..0 + 6..8 + -1] -------------------------------------------------------------------------------- /Chapter05/7.6.1.RemovingElements_NullElements.ps1: -------------------------------------------------------------------------------- 1 | $myArray = 1, 2, 3, 4, 5 2 | $myArray[1] = $null 3 | $myArray -------------------------------------------------------------------------------- /Chapter05/7.6.2.RemovingElements_NullElements_Count.ps1: -------------------------------------------------------------------------------- 1 | $myArray.Count 2 | 3 | # Expects output: 4 | # 5 | # 5 -------------------------------------------------------------------------------- /Chapter05/7.6.3.RemovingElements_NullElements_Count.ps1: -------------------------------------------------------------------------------- 1 | $myArray.Count 2 | 3 | # Expects output: 4 | # 5 | # 5 -------------------------------------------------------------------------------- /Chapter06/1.1.2.ImplicitBoolean.ps1: -------------------------------------------------------------------------------- 1 | $value = 1 2 | if ($value) { 3 | Write-Host 'Implicit true' 4 | } 5 | -------------------------------------------------------------------------------- /Chapter07/5.2.5.Alternatives.ps1: -------------------------------------------------------------------------------- 1 | New-Object System.Text.StringBuilder(10) 2 | [System.Text.StringBuilder]::new(10) -------------------------------------------------------------------------------- /Chapter08/1.1.5.AddDaysMethodArrayOfDateTime.ps1: -------------------------------------------------------------------------------- 1 | ((Get-Date '01/01/2017'), (Get-Date '01/02/2017')).AddDays(5) -------------------------------------------------------------------------------- /Chapter08/1.1.6.DayOfWeekPropertyArrayOfDateTime.ps1: -------------------------------------------------------------------------------- 1 | ((Get-Date '01/01/2017'), (Get-Date '01/02/2017')).DayOfWeek -------------------------------------------------------------------------------- /Chapter08/1.10.2.StartsWithAndEndsWith.ps1: -------------------------------------------------------------------------------- 1 | $string = 'abc' 2 | $string.StartsWith('ab') 3 | $string.EndsWith('bc') -------------------------------------------------------------------------------- /Chapter08/1.5.1.Trim.ps1: -------------------------------------------------------------------------------- 1 | $string = " This string has leading and trailing white space " 2 | $string.Trim() -------------------------------------------------------------------------------- /Chapter08/3.2.1.PowerOf10Operator.ps1: -------------------------------------------------------------------------------- 1 | 2e2 # Returns 200 (2 * 10^2) 2 | 2e-1 # Returns 0.2 (2 * 10^-1) 3 | -------------------------------------------------------------------------------- /Chapter09/1.2.1.SpecialCharacters.ps1: -------------------------------------------------------------------------------- 1 | '9*8'-match '\*' # * is reserved 2 | '1+5' -match '\+' # + is reserved -------------------------------------------------------------------------------- /Chapter09/1.5.6.RemoveAllBeforeCharacter.ps1: -------------------------------------------------------------------------------- 1 | 'Domain\User' -replace '.+\\' # Everything up to and including \ -------------------------------------------------------------------------------- /Chapter09/1.5.7.RemoveAllAfterCharacter.ps1: -------------------------------------------------------------------------------- 1 | 'Domain\User' -replace '\\.+' # Everything including and after \ 2 | -------------------------------------------------------------------------------- /Chapter09/4.1.5.Range_HexadecimalNumber_CaseSensitive.ps1: -------------------------------------------------------------------------------- 1 | 'The registry value is 0xAF9B7' -cmatch '0x[0-9a-fA-F]+' -------------------------------------------------------------------------------- /Chapter09/4.3.2.ClassSubtraction_UsingSpecificRanges.ps1: -------------------------------------------------------------------------------- 1 | 'The lazy cat sat on the mat' -match '[b-df-hj-np-tv-z]' -------------------------------------------------------------------------------- /Chapter10/4.1.2_Set-ItemProperty.ps1: -------------------------------------------------------------------------------- 1 | Set-ItemProperty -Path HKCU:\Environment -Name NewValue -Value 'New' 2 | -------------------------------------------------------------------------------- /Chapter11/2.1.2_WQL_SELECT_WHERE.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE ProcessID=$PID" -------------------------------------------------------------------------------- /Chapter12/3.11.1_ISEHelp.ps1: -------------------------------------------------------------------------------- 1 | Get-Item C:\Windows\System32\WindowsPowerShell\v1.0\modules\ISE\en-US\ISE-help.xml -------------------------------------------------------------------------------- /Chapter13/1.5.4_Chain.ps1: -------------------------------------------------------------------------------- 1 | $certValidation.Chain.ChainElements | Select-Object -ExpandProperty Certificate 2 | -------------------------------------------------------------------------------- /Chapter13/3.4.4_NamespaceProperty.ps1: -------------------------------------------------------------------------------- 1 | ('{0}.SearchCondition' -f $service.GetType().Namespace -as [Type])::new() -------------------------------------------------------------------------------- /Chapter15/3.5.4.RunspacePool_CleaningUp.ps1: -------------------------------------------------------------------------------- 1 | $jobs.Instance | ForEach-Object Dispose 2 | $runspacePool.Dispose() -------------------------------------------------------------------------------- /Chapter20/2.10.2_SimpleMethod.ps1: -------------------------------------------------------------------------------- 1 | [PSCustomObject]@{} | Add-Member MethodName -MemberType ScriptMethod -Value { } -------------------------------------------------------------------------------- /Chapter01/4.4.5_Update-Help_NamedCulture.ps1: -------------------------------------------------------------------------------- 1 | Update-Help -SourcePath C:\Temp -Module DnsClient -UICulture en-US 2 | -------------------------------------------------------------------------------- /Chapter03/3.3.2.Where-Object_OddOrder.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Where-Object -Property StartTime -Value (Get-Date 17:00:00) -gt -------------------------------------------------------------------------------- /Chapter04/7.2.1.IsAndIsNot.ps1: -------------------------------------------------------------------------------- 1 | 'string' -is [String] 2 | 1 -is [Int32] 3 | [String] -is [Type] 4 | 123 -isnot [String] -------------------------------------------------------------------------------- /Chapter05/7.8.5.JaggedArray_AccessingElements.ps1: -------------------------------------------------------------------------------- 1 | $arrayOfArrays[1][5] 2 | 3 | # Expects output: 4 | # 5 | # 9 6 | -------------------------------------------------------------------------------- /Chapter05/9.2.9.AccessByKey_StronglyTypedKey_Usage.ps1: -------------------------------------------------------------------------------- 1 | $dictionary."1" 2 | $dictionary[1] 3 | $dictionary["1"] 4 | -------------------------------------------------------------------------------- /Chapter05/9.3.4.EnqueueMethod.ps1: -------------------------------------------------------------------------------- 1 | $queue.Enqueue("Tom") 2 | $queue.Enqueue("Richard") 3 | $queue.Enqueue("Harry") 4 | -------------------------------------------------------------------------------- /Chapter07/6.3.2.StaticMethodInvocation.ps1: -------------------------------------------------------------------------------- 1 | [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() -------------------------------------------------------------------------------- /Chapter08/1.5.2.Trim_ArrayOfChar.ps1: -------------------------------------------------------------------------------- 1 | $string = '*__This string is surrounded by clutter.--#' 2 | $string.Trim('*_-#') -------------------------------------------------------------------------------- /Chapter08/5.4.2.Get-Date_MethodChaining.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).ToUniversalTime().Date.AddDays(-7).ToString('dd/MM/yyyy HH:mm') -------------------------------------------------------------------------------- /Chapter09/1.5.2.EscapingTheEscapeCharacter.ps1: -------------------------------------------------------------------------------- 1 | 'domain\user' -match 'domain\\user' 2 | 'domain\user' -match '.*\\.*' -------------------------------------------------------------------------------- /Chapter10/7.2.1_Test-FileCatalog.ps1: -------------------------------------------------------------------------------- 1 | Test-FileCatalog -Path C:\Temp\ACL -CatalogFilePath C:\Temp\Security\example.cat -------------------------------------------------------------------------------- /Chapter17/2.2.11.ValidatePattern_Options_Multiple.ps1: -------------------------------------------------------------------------------- 1 | [ValidatePattern('^Hello', Options = 'IgnoreCase, Multiline')] -------------------------------------------------------------------------------- /Chapter17/3.1.5.ValueFromPipeine_Where-Object_Filtering.ps1: -------------------------------------------------------------------------------- 1 | Get-EmptyOutput | Where-Object { $_ } | Get-InputObject -------------------------------------------------------------------------------- /Chapter19/2.2.2.ModuleBuilder_InModuleFolder.ps1: -------------------------------------------------------------------------------- 1 | Set-Location C:\Workspace\LocalMachine\LocalMachine 2 | Build-Module -------------------------------------------------------------------------------- /Chapter01/8.1.2_Splatting_CallOperator.ps1: -------------------------------------------------------------------------------- 1 | $getProcess = @{ 2 | Name = 'explorer' 3 | } 4 | & 'Get-Process' @getProcess -------------------------------------------------------------------------------- /Chapter01/8.1.3_Splatting_CallOperator.ps1: -------------------------------------------------------------------------------- 1 | $getProcess = @{ 2 | Name = 'explorer' 3 | } 4 | & 'Get-Process' @getProcess -------------------------------------------------------------------------------- /Chapter02/1.1.1_New-Module.ps1: -------------------------------------------------------------------------------- 1 | New-Module -Name TestModule -ScriptBlock { 2 | function Get-Number { return 1 } 3 | } -------------------------------------------------------------------------------- /Chapter03/3.2.1.ForEach-Object.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | ForEach-Object { 2 | Write-Host $_.Name -ForegroundColor Green 3 | } -------------------------------------------------------------------------------- /Chapter04/3.3.1.Equal_Examples.ps1: -------------------------------------------------------------------------------- 1 | 1 -eq 1 2 | 'string' -eq 'string' 3 | [char]'a' -eq 'A' 4 | $true -eq 1 5 | $false -eq 0 -------------------------------------------------------------------------------- /Chapter04/3.6.1.ContainsAndNotContains.ps1: -------------------------------------------------------------------------------- 1 | 1, 2 -contains 2 # Returns true 2 | 1, 2, 3 -contains 4 # Returns fals -------------------------------------------------------------------------------- /Chapter04/4.3.1.Split.ps1: -------------------------------------------------------------------------------- 1 | 'a1b2c3d4' -split '[0-9]' 2 | 3 | # Expects output: 4 | # 5 | # a 6 | # b 7 | # c 8 | # d 9 | -------------------------------------------------------------------------------- /Chapter04/9.5.1.Join.ps1: -------------------------------------------------------------------------------- 1 | "a,b,c,d" -split ',' -join "`t" 2 | 3 | # Expects output: 4 | # 5 | # a b c d -------------------------------------------------------------------------------- /Chapter05/5.1.1.TypeConversion_ToString.ps1: -------------------------------------------------------------------------------- 1 | [String](Get-Date) 2 | 3 | # Expects output: 4 | # 5 | # 10/27/2016 13:14:32 -------------------------------------------------------------------------------- /Chapter05/6.1.3.ReferenceTypeAssignment.ps1: -------------------------------------------------------------------------------- 1 | $object1 = $object2 = [PSCustomObject]@{ 2 | Name = 'First object' 3 | } -------------------------------------------------------------------------------- /Chapter05/7.8.2.MultiDimensionalArray_AccessingElements.ps1: -------------------------------------------------------------------------------- 1 | $arrayOfArrays[0][1] 2 | 3 | # Expects output: 4 | # 5 | # 2 -------------------------------------------------------------------------------- /Chapter05/7.8.3.MultiDimensionalArray_AccessingElements_2.ps1: -------------------------------------------------------------------------------- 1 | $arrayOfArrays[1][2] 2 | 3 | # Expects output: 4 | # 5 | # 6 -------------------------------------------------------------------------------- /Chapter05/9.2.7.AccessByKey.ps1: -------------------------------------------------------------------------------- 1 | $dictionary["Computer1"] # Key reference 2 | $dictionary.Computer1 # Dot-notation -------------------------------------------------------------------------------- /Chapter06/2.2.2.for_Step.ps1: -------------------------------------------------------------------------------- 1 | for ($i = 0; $i -lt $processes.Count; $i += 3) { 2 | Write-Host $processes[$i].Name 3 | } -------------------------------------------------------------------------------- /Chapter08/3.2.5.Math_CeilingAndFloor.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Ceiling(2.1234) # Returns 3 2 | [Math]::Floor(2.9876) # Returns 2 -------------------------------------------------------------------------------- /Chapter08/5.5.1.ComparingDates.ps1: -------------------------------------------------------------------------------- 1 | $date1 = (Get-Date).AddDays(-20) 2 | $date2 = (Get-Date).AddDays(1) 3 | $date2 -gt $date1 -------------------------------------------------------------------------------- /Chapter10/7.1.1_New-FileCatalog.ps1: -------------------------------------------------------------------------------- 1 | New-FileCatalog -Path C:\Temp\ACL -CatalogFilePath C:\Temp\Security\example.cat 2 | -------------------------------------------------------------------------------- /Chapter11/2.1.3_WQL_Escape_characters.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_Process -Filter "ExecutablePath='C:\\Windows\\Explorer.exe'" -------------------------------------------------------------------------------- /Chapter11/3.5.2_CimInstance_new.ps1: -------------------------------------------------------------------------------- 1 | $class = Get-CimClass Win32_ProcessStartup 2 | $startupInfo = [CimInstance]::new($class) -------------------------------------------------------------------------------- /Chapter11/5.1.3_ShareSecurity.ps1: -------------------------------------------------------------------------------- 1 | $security = Get-CimInstance Win32_LogicalShareSecuritySetting -Filter "Name='WmiPerms'" -------------------------------------------------------------------------------- /Chapter14/2.3.10_Reconnect.ps1: -------------------------------------------------------------------------------- 1 | $session = Get-PSSession -ComputerName PSTest -Name 'Example' 2 | Connect-PSSession $session -------------------------------------------------------------------------------- /Chapter15/3.1.2.AddCommand.ps1: -------------------------------------------------------------------------------- 1 | [PowerShell]::Create().AddCommand('Get-Process').AddParameter('Name', 'powershell').Invoke() -------------------------------------------------------------------------------- /Chapter16/3.5.1_CmdletBinding_empty_param.ps1: -------------------------------------------------------------------------------- 1 | function Test-EmptyParam { 2 | [CmdletBinding()] 3 | param ( ) 4 | } -------------------------------------------------------------------------------- /Chapter17/4.1.4.PositionAndParameterSetName.ps1: -------------------------------------------------------------------------------- 1 | [Parameter(Position = 1, ParameterSetName = 'FirstSetName')] 2 | $Parameter1 -------------------------------------------------------------------------------- /Chapter18/2.1.2.class_new.ps1: -------------------------------------------------------------------------------- 1 | [MyClass]::new() 2 | 3 | # Expects output: 4 | # 5 | # Property 6 | # -------- 7 | # Value -------------------------------------------------------------------------------- /Chapter20/2.6.3_AfterAll.ps1: -------------------------------------------------------------------------------- 1 | AfterAll { 2 | Pop-Location 3 | Remove-Item C:\Temp\StaleFiles -Recurse -Force 4 | } -------------------------------------------------------------------------------- /Chapter03/4.2.4.Sort-Object_MultipleProperties.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\Windows\System32 | 2 | Sort-Object LastWriteTime, Name -------------------------------------------------------------------------------- /Chapter03/5.1.3.Group-Object_Property.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\Windows\Assembly -Filter *.dll -Recurse | 2 | Group-Object Name -------------------------------------------------------------------------------- /Chapter04/1.6.2.Remainder_Alternation.ps1: -------------------------------------------------------------------------------- 1 | 1..100 | Where-Object {$_ % 5 -eq 0 } | ForEach-Object { 2 | Write-Host $_ 3 | } -------------------------------------------------------------------------------- /Chapter04/3.2.4.ImplicitBoolean.ps1: -------------------------------------------------------------------------------- 1 | [Boolean]@($null) # Returns false 2 | [Boolean]@($null, $null) # Returns true -------------------------------------------------------------------------------- /Chapter04/3.2.5.NullEqualsArray.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2, $null, $null 2 | if ($null -eq $array) { Write-Host 'Variable not set' } 3 | -------------------------------------------------------------------------------- /Chapter05/1.1.3.Interpolation.ps1: -------------------------------------------------------------------------------- 1 | $var = 'var' 2 | "$variable" # Will not expand correctly 3 | "${var}iable" # Will expand var -------------------------------------------------------------------------------- /Chapter05/5.1.3.AssignedType.ps1: -------------------------------------------------------------------------------- 1 | [String]$thisString = "some value" 2 | [Int]$thisNumber = 2 3 | [DateTime]$date = '01/01/2016' -------------------------------------------------------------------------------- /Chapter05/7.6.5.RemovingElements_NullElements_Where-Object.ps1: -------------------------------------------------------------------------------- 1 | $myArray | Where-Object { $_ } | ForEach-Object { Write-Host $_ } -------------------------------------------------------------------------------- /Chapter05/7.8.1.MultiDimensionalArray.ps1: -------------------------------------------------------------------------------- 1 | $arrayOfArrays = @( 2 | @(1, 2, 3), 3 | @(4, 5, 6), 4 | @(7, 8, 9) 5 | ) -------------------------------------------------------------------------------- /Chapter05/8.1.3.CreateHashtableWithKeys_Multiline.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ 2 | Key1 = "Value1" 3 | Key2 = "Value2" 4 | } 5 | -------------------------------------------------------------------------------- /Chapter06/1.2.4.Switch_Array.ps1: -------------------------------------------------------------------------------- 1 | switch (1, 2) { 2 | 1 { Write-Host 'Equals 1' } 3 | 2 { Write-Host 'Equals 2' } 4 | } -------------------------------------------------------------------------------- /Chapter06/2.4.1.while.ps1: -------------------------------------------------------------------------------- 1 | while (-not (Test-Path $env:TEMP\test.txt -PathType Leaf)) { 2 | Start-Sleep -Seconds 10 3 | } 4 | -------------------------------------------------------------------------------- /Chapter07/5.2.15.ChainCasting.ps1: -------------------------------------------------------------------------------- 1 | using namespace System.IO 2 | 3 | [BinaryReader][MemoryStream][Byte[]][Char[]]'hello world' -------------------------------------------------------------------------------- /Chapter08/1.3.8.ForceSplitString.ps1: -------------------------------------------------------------------------------- 1 | $string = 'one||two||three' 2 | $string.Split([String[]]'||', [StringSplitOptions]::None) -------------------------------------------------------------------------------- /Chapter08/1.9.2.ToLowerAndReplace.ps1: -------------------------------------------------------------------------------- 1 | $string = 'AbN' 2 | $string = $string.ToLower() 3 | $string = $string.Replace('n', 'c') -------------------------------------------------------------------------------- /Chapter08/2.3.1.Convert-String.ps1: -------------------------------------------------------------------------------- 1 | 'Michael Caine', 'Benny Hill', 'Raf Vallone' | Convert-String -Example 'Michael Caine=MCaine' -------------------------------------------------------------------------------- /Chapter08/5.3.3.Get-Date_AddDays.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).AddDays(1) # One day from now 2 | (Get-Date).AddDays(-1) # One day before now -------------------------------------------------------------------------------- /Chapter08/5.4.1.Get-Date_Format_And_ToString.ps1: -------------------------------------------------------------------------------- 1 | Get-Date -Format 'dd/MM/yyyy HH:mm' 2 | (Get-Date).ToString('dd/MM/yyyy HH:mm') -------------------------------------------------------------------------------- /Chapter10/3.2.5_FileAttribute_bor.ps1: -------------------------------------------------------------------------------- 1 | $file = Get-Item 'somefile.txt' 2 | $file.Attributes = $file.Attributes -bor 'ReadOnly' -------------------------------------------------------------------------------- /Chapter10/7.2.5_Test-FileCatalog_Removed.ps1: -------------------------------------------------------------------------------- 1 | $result.CatalogItems.Keys | Where-Object { -not $result.PathItems.ContainsKey($_) } -------------------------------------------------------------------------------- /Chapter14/2.4.2_Enter-PSSession_Session.ps1: -------------------------------------------------------------------------------- 1 | $session = New-PSSession -ComputerName PSTest 2 | Enter-PSSession -Session $session -------------------------------------------------------------------------------- /Chapter15/3.8.1.SynchronizedHashtable.ps1: -------------------------------------------------------------------------------- 1 | $synchronizedHashtable = [Hashtable]::Synchronized(@{ 2 | Key = 'Value' 3 | }) -------------------------------------------------------------------------------- /Chapter15/3.8.3.SynchronizedArrayList.ps1: -------------------------------------------------------------------------------- 1 | [System.Collections.ArrayList]::Synchronized([System.Collections.ArrayList]::new()) -------------------------------------------------------------------------------- /Chapter16/2.4.2_Invoke-Return_running.ps1: -------------------------------------------------------------------------------- 1 | 1..10 | Invoke-Return 2 | 3 | # Expects output: 4 | # 5 | # 1 6 | # 2 7 | # All done -------------------------------------------------------------------------------- /Chapter16/3.2.2_param_nullable.ps1: -------------------------------------------------------------------------------- 1 | function Test-Nullable { 2 | param ( 3 | [Nullable[DateTime]]$Date 4 | ) 5 | } -------------------------------------------------------------------------------- /Chapter20/1.1.2_Ast_ScriptBlock.ps1: -------------------------------------------------------------------------------- 1 | function Write-Content { Write-Host 'content' } 2 | (Get-Command Write-Content).ScriptBlock -------------------------------------------------------------------------------- /Chapter20/1.1.4_Get-Item.ps1: -------------------------------------------------------------------------------- 1 | function Write-Content { Write-Host 'content' } 2 | (Get-Item function:\Write-Content).ScriptBlock -------------------------------------------------------------------------------- /Chapter20/2.10.15_New-CimInstance_WithNamespace.ps1: -------------------------------------------------------------------------------- 1 | New-CimInstance MSFT_NetAdapter -Namespace ROOT/StandardCimv2 -ClientOnly -------------------------------------------------------------------------------- /Chapter20/2.9.4_DemoFunction.ps1: -------------------------------------------------------------------------------- 1 | function Get-OperatingSystemName { 2 | (Get-CimInstance Win32_OperatingSystem).Caption 3 | } -------------------------------------------------------------------------------- /Chapter03/2.3.3.Method_arguments.ps1: -------------------------------------------------------------------------------- 1 | $tcpClient = New-Object System.Net.Sockets.TcpClient 2 | $tcpClient.Connect("127.0.0.1", 135) -------------------------------------------------------------------------------- /Chapter03/2.3.6.Method_ToString.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date).ToString('u') 2 | 3 | # Expects output similar to: 4 | # 5 | # 2016-12-08 21:18:49Z -------------------------------------------------------------------------------- /Chapter03/4.2.1.Sort-Object.ps1: -------------------------------------------------------------------------------- 1 | 5, 4, 3, 2, 1 | Sort-Object 2 | 3 | # Expects output: 4 | # 5 | # 1 6 | # 2 7 | # 3 8 | # 4 9 | # 5 -------------------------------------------------------------------------------- /Chapter04/6.3.1.ExclusiveOr.ps1: -------------------------------------------------------------------------------- 1 | $true -xor $false 2 | 1 -le 2 -xor 1 -eq 2 3 | (Test-Path C:\Windows) -xor (Test-Path D:\Windows) 4 | -------------------------------------------------------------------------------- /Chapter05/5.1.2.TypeConversion_ToDateTime.ps1: -------------------------------------------------------------------------------- 1 | [DateTime]"01/01/2016" 2 | 3 | # Expects output: 4 | # 5 | # 01 January 2016 00:00:00 -------------------------------------------------------------------------------- /Chapter05/7.8.4.JaggedArray.ps1: -------------------------------------------------------------------------------- 1 | $arrayOfArrays = @( 2 | @(1, 2), 3 | @(4, 5, 6, 7, 8, 9), 4 | @(10, 11, 12) 5 | ) -------------------------------------------------------------------------------- /Chapter05/8.4.4.Loop.ps1: -------------------------------------------------------------------------------- 1 | foreach ($key in $hashtable.Keys) { 2 | Write-Host "Key: $key Value: $($hashtable[$key])" 3 | } 4 | -------------------------------------------------------------------------------- /Chapter05/9.4.4.PushMethod.ps1: -------------------------------------------------------------------------------- 1 | $stack.Push("Up the road") 2 | $stack.Push("Over the gate") 3 | $stack.Push("Under the bridge") 4 | -------------------------------------------------------------------------------- /Chapter06/2.2.3.for_Step_Decrement.ps1: -------------------------------------------------------------------------------- 1 | for ($i = $processes.Count - 1; $i -ge 0; $i--) { 2 | Write-Host $processes[$i].Name 3 | } -------------------------------------------------------------------------------- /Chapter08/1.7.2.IndexOfAndIf.ps1: -------------------------------------------------------------------------------- 1 | $string = 'abcdef' 2 | if ($string.IndexOf('a') -gt -1) { 3 | 'The string contains an a' 4 | } -------------------------------------------------------------------------------- /Chapter08/1.9.3.ToTitleCase.ps1: -------------------------------------------------------------------------------- 1 | (Get-Culture).TextInfo.ToTitleCase('some title') 2 | 3 | # Expects output: 4 | # 5 | # Some Title -------------------------------------------------------------------------------- /Chapter09/1.4.3.Repetition_OneOrMore.ps1: -------------------------------------------------------------------------------- 1 | 'aaabc' -match 'a+' # Returns true, matches 'aaa' 2 | 'bcd' -match 'a+' # Returns false -------------------------------------------------------------------------------- /Chapter09/4.1.2.Range.ps1: -------------------------------------------------------------------------------- 1 | '1st place' -match '[0-9]+' # $matches[0] is "1" 2 | '23rd place' -match '[0-9]+' # $matches[0] is "23" -------------------------------------------------------------------------------- /Chapter09/5.1.3.Alternation_WIthAnchors.ps1: -------------------------------------------------------------------------------- 1 | 'one', 'one hundred', 'three', 'eighty three' | Where-Object { $_ -match '^one$|^three$' } -------------------------------------------------------------------------------- /Chapter10/2.2.3_Remove-Item.ps1: -------------------------------------------------------------------------------- 1 | $file = New-TemporaryFile 2 | Set-Content -Path $file -Value 'Temporary: 10' 3 | Remove-Item $file -------------------------------------------------------------------------------- /Chapter10/3.2.4_FileAttributes_bxor.ps1: -------------------------------------------------------------------------------- 1 | $file = Get-Item 'somefile.txt' 2 | $file.Attributes = $file.Attributes -bxor 'ReadOnly' -------------------------------------------------------------------------------- /Chapter10/3.2.7_FileAttribute_AsString.ps1: -------------------------------------------------------------------------------- 1 | $file = Get-Item 'somefile.txt' 2 | $file.Attributes = "$($file.Attributes), ReadOnly" -------------------------------------------------------------------------------- /Chapter10/7.2.4_Test-FileCatalog_Added.ps1: -------------------------------------------------------------------------------- 1 | $result.PathItems.Keys | Where-Object { -not $result.CatalogItems.ContainsKey($_) } 2 | -------------------------------------------------------------------------------- /Chapter11/2.2.2_Wmi_object_path.ps1: -------------------------------------------------------------------------------- 1 | (Get-CimClass Win32_Process).CimClassProperties | 2 | Where-Object { $_.Flags -band 'Key' } -------------------------------------------------------------------------------- /Chapter13/1.1.1_Invoke-WebRequest.ps1: -------------------------------------------------------------------------------- 1 | Invoke-WebRequest https://blogs.technet.microsoft.com/heyscriptingguy/ -UseBasicParsing 2 | -------------------------------------------------------------------------------- /Chapter13/1.3.2_Invoke-WebRequest_InvalidCertificate_PSCore.ps1: -------------------------------------------------------------------------------- 1 | Invoke-WebRequest https://expired.badssl.com/ -SkipCertificateCheck -------------------------------------------------------------------------------- /Chapter14/2.3.2_Invoke-Command_MultipleComputers.ps1: -------------------------------------------------------------------------------- 1 | Invoke-Command { Get-Process -Id $PID } -ComputerName 'first', 'second', 'third' -------------------------------------------------------------------------------- /Chapter14/5.1.1_New-PSSession_CredSSP.ps1: -------------------------------------------------------------------------------- 1 | New-PSSession -ComputerName PSTest -Credential (Get-Credential) -Authentication CredSSP 2 | -------------------------------------------------------------------------------- /Chapter17/1.2.2.PositionalBinding.ps1: -------------------------------------------------------------------------------- 1 | Test-Position 1 2 2 | 3 | # Requires function from previous example. Expects output: 4 | # 1-2 -------------------------------------------------------------------------------- /Chapter20/2.8.2_Mock_MockWith.ps1: -------------------------------------------------------------------------------- 1 | Describe Subject { 2 | Mock Get-Date { 3 | [DateTime]::new(2017, 1, 1) 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter21/4.1.1_TryCatch.ps1: -------------------------------------------------------------------------------- 1 | try { 2 | $null.ToString() 3 | } catch { 4 | Write-Host 'This exception has been handled' 5 | } -------------------------------------------------------------------------------- /Chapter03/2.1.2.Get-Member_MemberType.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Id $PID | Get-Member -MemberType Property 2 | 3 | # Expects property members only -------------------------------------------------------------------------------- /Chapter03/2.3.7.Get-Date_Format_alternative.ps1: -------------------------------------------------------------------------------- 1 | Get-Date -Format u 2 | 3 | # Expects output similar to: 4 | # 5 | # 2016-12-08 21:19:31Z -------------------------------------------------------------------------------- /Chapter03/2.4.3.AccessModifiers_Set.ps1: -------------------------------------------------------------------------------- 1 | $File = New-Item NewFile.txt -Force 2 | $File.CreationTime = Get-Date -Day 1 -Month 2 -Year 1692 -------------------------------------------------------------------------------- /Chapter03/3.3.3.Where-Object_FilterScript.ps1: -------------------------------------------------------------------------------- 1 | Get-Service | Where-Object { $_.StartType -eq 'Manual' -and $_.Status -eq 'Running' } 2 | -------------------------------------------------------------------------------- /Chapter05/7.1.5.ArrayOfElements_Multiline.ps1: -------------------------------------------------------------------------------- 1 | $myGreetings = "Hello world", 2 | "Hello sun", 3 | "Hello moon" -------------------------------------------------------------------------------- /Chapter05/7.3.3.AddElement_Equivalent.ps1: -------------------------------------------------------------------------------- 1 | $firstArray = 1, 2, 3 2 | $secondArray = 4, 5, 6 3 | $mergedArray = $firstArray + $secondArray -------------------------------------------------------------------------------- /Chapter05/7.5.2.UpdatingInLoop.ps1: -------------------------------------------------------------------------------- 1 | $myArray = 1, 2, 3, 4, 5 2 | for ($i = 0; $i -lt $myArray.Count; $i++) { 3 | $myArray[$i] = 9 4 | } -------------------------------------------------------------------------------- /Chapter05/9.2.13.Loop.ps1: -------------------------------------------------------------------------------- 1 | foreach ($key in $dictionary.Keys) { 2 | Write-Host "Key: $key Value: $($dictionary[$key])" 3 | } 4 | -------------------------------------------------------------------------------- /Chapter07/5.2.13.newMethod_ArgumentList_Working.ps1: -------------------------------------------------------------------------------- 1 | [Byte[]]$bytes = 97, 98, 99 2 | $memoryStream = [System.IO.MemoryStream]::new($bytes) -------------------------------------------------------------------------------- /Chapter08/1.2.1.Substring.ps1: -------------------------------------------------------------------------------- 1 | $myString = 'abcdefghijklmnopqrstuvwxyz' 2 | $myString.Substring(20) # Start at index 20. Returns 'uvwxyz' -------------------------------------------------------------------------------- /Chapter08/1.5.4.TrimEndTooFar.ps1: -------------------------------------------------------------------------------- 1 | $string = 'magnet.uk.net' 2 | $string.TrimEnd('.uk.net') 3 | 4 | # Expects output: 5 | # 6 | # mag 7 | -------------------------------------------------------------------------------- /Chapter09/2.1.2.ReplaceTooMuch.ps1: -------------------------------------------------------------------------------- 1 | 'The first day is Monday' -replace 'day', 'night' 2 | 'Monday is the first day' -replace 'day', 'night' -------------------------------------------------------------------------------- /Chapter10/2.1.2_Test-Path_PathType.ps1: -------------------------------------------------------------------------------- 1 | Test-Path C:\Windows -PathType Container 2 | Test-Path C:\Windows\System32\cmd.exe -PathType Leaf -------------------------------------------------------------------------------- /Chapter10/3.1.1_Set-ItemProperty.ps1: -------------------------------------------------------------------------------- 1 | Set-ItemProperty .\somefile.txt -Name IsReadOnly -Value $true 2 | -------------------------------------------------------------------------------- /Chapter10/4.1.4_New-ItemProperty.ps1: -------------------------------------------------------------------------------- 1 | New-ItemProperty HKCU:\Environment -Name Expand -Value 'User: %USERNAME%' -PropertyType ExpandString -------------------------------------------------------------------------------- /Chapter10/5.6.3_Get-Acl.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\3 2 | $acl.Access | Select-Object IdentityReference, FileSystemRights, IsInherited -------------------------------------------------------------------------------- /Chapter10/5.8.4_AddAccessRule.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\5 2 | $acl.AddAccessRule($ace) 3 | Set-Acl C:\Temp\ACL\5 -AclObject $acl -------------------------------------------------------------------------------- /Chapter11/5.1.4_Descriptor.ps1: -------------------------------------------------------------------------------- 1 | $return = $security | Invoke-CimMethod -MethodName GetSecurityDescriptor 2 | $aclObject = $return.Descriptor -------------------------------------------------------------------------------- /Chapter11/5.2.1_WmiPermissions.ps1: -------------------------------------------------------------------------------- 1 | Get-CimClass __SystemSecurity -Namespace root 2 | Get-CimClass __SystemSecurity -Namespace root\cimv2 -------------------------------------------------------------------------------- /Chapter14/2.3.7_InDisconnectedSession.ps1: -------------------------------------------------------------------------------- 1 | Invoke-Command { Start-Sleep -Seconds 120; 'Done' } -ComputerName PSTest -InDisconnectedSession -------------------------------------------------------------------------------- /Chapter16/1.5.2_short_pipeline.ps1: -------------------------------------------------------------------------------- 1 | Get-Service | Where-Object Status -eq Running | ForEach-Object { 2 | # Do work on the service 3 | } -------------------------------------------------------------------------------- /Chapter16/2.1.2_Show-Pipeline_begin.ps1: -------------------------------------------------------------------------------- 1 | function Show-Pipeline { 2 | begin { 3 | Write-Host 'Pipeline start' 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter17/2.1.1.PSTypeNameAttribute.ps1: -------------------------------------------------------------------------------- 1 | $object = [PSCustomObject]@{ 2 | Property = 'Value' 3 | PSTypeName = 'SomeTypeName' 4 | } -------------------------------------------------------------------------------- /Chapter18/1.1.2.enum_duplicate_values.ps1: -------------------------------------------------------------------------------- 1 | enum MyEnum { 2 | One = 1 3 | First = 1 4 | Two = 2 5 | Second = 2 6 | } -------------------------------------------------------------------------------- /Chapter18/1.5.1.enum_flags.ps1: -------------------------------------------------------------------------------- 1 | [Flags()] 2 | enum MyEnum { 3 | First = 1 # 001 4 | Second = 2 # 010 5 | Third = 4 # 100 6 | } -------------------------------------------------------------------------------- /Chapter20/1.1.3_Get-Command_Definition.ps1: -------------------------------------------------------------------------------- 1 | function Write-Content { Write-Host 'content' } 2 | (Get-Command Write-Content).ScriptBlock -------------------------------------------------------------------------------- /Chapter20/2.7.1_TestDrive_BeforeAll.ps1: -------------------------------------------------------------------------------- 1 | BeforeAll { 2 | $extensions = '.txt', '.log', '.doc' 3 | Push-Location 'TestDrive:\' 4 | } -------------------------------------------------------------------------------- /Chapter21/3.1.2_ScriptStackTrace.ps1: -------------------------------------------------------------------------------- 1 | $record.ScriptStackTrace 2 | 3 | # Expects output: 4 | # at , : line 1 5 | -------------------------------------------------------------------------------- /Chapter21/3.2.2_Write-Error_CategoryAndErrorId.ps1: -------------------------------------------------------------------------------- 1 | Write-Error -Message 'Message' -Category 'InvalidOperation' -ErrorId 'UniqueID' 2 | -------------------------------------------------------------------------------- /Chapter21/4.2.1_Re-throw.ps1: -------------------------------------------------------------------------------- 1 | try { 2 | 'Statement1' 3 | throw 'Statement2' 4 | 'Statement3' 5 | } catch { 6 | throw 7 | } -------------------------------------------------------------------------------- /Chapter03/2.3.2.Method_invoke.ps1: -------------------------------------------------------------------------------- 1 | $date = Get-Date "01/01/2010" 2 | $date.ToLongDateString() 3 | 4 | # Expects: 5 | # 6 | # 01 January 2010 -------------------------------------------------------------------------------- /Chapter05/8.2.3.ContainsMethod.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{} 2 | if (-not $hashtable.Contains("Key1")) { 3 | $hashtable.Add("Key1", "Value1") 4 | } -------------------------------------------------------------------------------- /Chapter05/8.2.7.AddOnly.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ Existing = "Old" } 2 | if (-not $hashtable.Contains("New")) { 3 | $hashtable.New = "New" 4 | } -------------------------------------------------------------------------------- /Chapter05/9.2.14.RemoveMethod.ps1: -------------------------------------------------------------------------------- 1 | foreach ($key in $dictionary.Keys) { 2 | Write-Host "Key: $key Value: $($dictionary[$key])" 3 | } 4 | -------------------------------------------------------------------------------- /Chapter05/9.2.3.ContainsKey.ps1: -------------------------------------------------------------------------------- 1 | if (-not $dictionary.ContainsKey("Computer2")) { 2 | $dictionary.Add("Computer2", "192.168.10.13") 3 | } -------------------------------------------------------------------------------- /Chapter06/2.2.1.for.ps1: -------------------------------------------------------------------------------- 1 | $processes = Get-Process 2 | for ($i = 0; $i -lt $processes.Count; $i++) { 3 | Write-Host $processes[$i].Name 4 | } -------------------------------------------------------------------------------- /Chapter07/6.2.3.StaticPropertyInvocation_FromVariable.ps1: -------------------------------------------------------------------------------- 1 | $type = [System.Text.Encoding] 2 | $propertyName = 'ASCII' 3 | $type::$propertyName -------------------------------------------------------------------------------- /Chapter08/2.3.2.Convert-String_AbstractExample.ps1: -------------------------------------------------------------------------------- 1 | 'Michael Caine', 'Benny Hill', 'Raf Vallone' | Convert-String -Example 'First Second=FSecond' -------------------------------------------------------------------------------- /Chapter09/5.1.4.Alternation_WIthAnchors_AndGroup.ps1: -------------------------------------------------------------------------------- 1 | 'one', 'one hundred', 'three', 'eighty three' | Where-Object { $_ -match '^(one|three)$' } -------------------------------------------------------------------------------- /Chapter10/7.2.6_Test-FileCatalog_Modified.ps1: -------------------------------------------------------------------------------- 1 | $result.PathItems.Keys | Where-Object { $result.CatalogItems[$_] -ne $result.PathItems[$_]} 2 | -------------------------------------------------------------------------------- /Chapter11/3.5.1_New-CimInstance.ps1: -------------------------------------------------------------------------------- 1 | $class = Get-CimClass Win32_ProcessStartup 2 | $startupInfo = New-CimInstance -CimClass $class -ClientOnly -------------------------------------------------------------------------------- /Chapter14/1.4.8_SkipCACheck.ps1: -------------------------------------------------------------------------------- 1 | $options = New-PSSessionOption -SkipCACheck 2 | $session = New-PSSession computerName -SessionOptions $options -------------------------------------------------------------------------------- /Chapter14/1.5.6_SIDTranslate.ps1: -------------------------------------------------------------------------------- 1 | using namespace System.Security.Principal 2 | 3 | ([NTAccount]"Administrator").Translate([SecurityIdentifier]) -------------------------------------------------------------------------------- /Chapter14/1.5.8_SetAcl.ps1: -------------------------------------------------------------------------------- 1 | $sddl = $acl.GetSddlForm('All') 2 | Set-PSSessionConfiguration Microsoft.PowerShell -SecurityDescriptorSddl $sddl -------------------------------------------------------------------------------- /Chapter17/3.2.3.ValueFromPipelineByPropertyName_MultipleParams_Binding.ps1: -------------------------------------------------------------------------------- 1 | [PSCustomObject]@{ Name = 'Name'; Status = 'Running' } | Get-Status -------------------------------------------------------------------------------- /Chapter17/4.1.3.DefaultParameterSetName.ps1: -------------------------------------------------------------------------------- 1 | # Cannot be run in isolation 2 | 3 | [CmdletBinding(DefaultParameterSetName = 'FirstSetName')] 4 | -------------------------------------------------------------------------------- /Chapter17/4.1.9.MultipleParameterSets_Exploration.ps1: -------------------------------------------------------------------------------- 1 | [System.Management.Automation.ProxyCommand]::GetParamBlock((Get-Command Get-Process)) 2 | -------------------------------------------------------------------------------- /Chapter19/4.2.3.Remove-Module.ps1: -------------------------------------------------------------------------------- 1 | Remove-Module TestOnRemove 2 | Test-Path variable:VariableName 3 | 4 | # Expects output: 5 | # 6 | # False -------------------------------------------------------------------------------- /Chapter21/5.2.3_WithErrorActionSilentlyContinue.ps1: -------------------------------------------------------------------------------- 1 | Invoke-Something -ErrorAction SilentlyContinue 2 | 3 | # Expects output: 4 | # No error -------------------------------------------------------------------------------- /Chapter03/4.2.2.Sort-Object_CaseInsensitive.ps1: -------------------------------------------------------------------------------- 1 | 'ccc', 'BBB', 'aaa' | Sort-Object 2 | 3 | # Expected output: 4 | # 5 | # aaa 6 | # BBB 7 | # ccc -------------------------------------------------------------------------------- /Chapter04/4.1.1.Match.ps1: -------------------------------------------------------------------------------- 1 | 'The cow jumped over the moon' -match 'cow' # Returns true 2 | 'The cow' -match 'The +cow' # Returns true -------------------------------------------------------------------------------- /Chapter04/5.3.2.BinaryExclusiveOr_Toggle.ps1: -------------------------------------------------------------------------------- 1 | 512 -bxor 2 # Result is 514 (Disabled, 2 is set) 2 | 514 -bxor 2 # Result is 512 (Enabled, 2 is not set -------------------------------------------------------------------------------- /Chapter04/9.5.2.JoinNoSeparator.ps1: -------------------------------------------------------------------------------- 1 | -join ('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd') 2 | 3 | # Expects output: 4 | # 5 | # hello world -------------------------------------------------------------------------------- /Chapter05/1.1.5.Assignment.ps1: -------------------------------------------------------------------------------- 1 | $itemCount = 7 2 | $dateFormat = "ddMMyyyy" 3 | $numbers = @(1, 9, 5, 2) 4 | $psProcess = Get-Process -Name PowerShell -------------------------------------------------------------------------------- /Chapter05/7.7.2.SetVariablesFromArray_Split.ps1: -------------------------------------------------------------------------------- 1 | $firstName, $lastName = "First Last" -split " " 2 | $firstName, $lastName = "First Last".Split(" ") -------------------------------------------------------------------------------- /Chapter05/9.1.11.Reassign.ps1: -------------------------------------------------------------------------------- 1 | $list = New-Object System.Collections.Generic.List[Int] 2 | $list.AddRange([Int[]](1, 2, 2, 4)) 3 | $list[2] = 3 4 | -------------------------------------------------------------------------------- /Chapter06/1.2.1.Switch.ps1: -------------------------------------------------------------------------------- 1 | $value = 1 2 | switch ($value) { 3 | 1 { Write-Host 'value is 1' } 4 | 1 { Write-Host 'value is still 1' } 5 | } -------------------------------------------------------------------------------- /Chapter07/8.1.2.TypeAccelerators_ADSI.ps1: -------------------------------------------------------------------------------- 1 | [System.DirectoryServices.DirectoryEntry]"WinNT://$env:COMPUTERNAME" 2 | [ADSI]"WinNT://$env:COMPUTERNAME" -------------------------------------------------------------------------------- /Chapter07/8.1.3.TypeAccelerators_Listing.ps1: -------------------------------------------------------------------------------- 1 | $type = [PowerShell].Assembly.GetType('System.Management.Automation.TypeAccelerators') 2 | $type::Get -------------------------------------------------------------------------------- /Chapter07/9.1.5.UsingAssembly_Specific.ps1: -------------------------------------------------------------------------------- 1 | using assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' -------------------------------------------------------------------------------- /Chapter09/2.1.3.WordBoundary.ps1: -------------------------------------------------------------------------------- 1 | 'The first day is Monday' -replace '\bday\b', 'night' 2 | 'Monday is the first day' -replace '\bday\b', 'night' 3 | -------------------------------------------------------------------------------- /Chapter09/4.1.8.Range_LIteralHyphen.ps1: -------------------------------------------------------------------------------- 1 | 'one-two_three,four' -split '[-_,]' 2 | 3 | # Expects output: 4 | # one 5 | # two 6 | # three 7 | # four -------------------------------------------------------------------------------- /Chapter09/6.1.6.Alternation_WordList.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem -Recurse -File | 2 | Where-Object { $_.Name -match '(pwd|pass(word|wd)?).*\.(txt|doc)$' } -------------------------------------------------------------------------------- /Chapter11/2.3.5_ASSOCIATORS_OF_ResultClass.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -Query "ASSOCIATORS OF {Win32_Process.Handle=$PID} WHERE ResultClass = CIM_DATAFILE" -------------------------------------------------------------------------------- /Chapter11/5.1.14_SetDescriptor.ps1: -------------------------------------------------------------------------------- 1 | $security | Invoke-CimMethod -MethodName SetSecurityDescriptor -Arguments @{ 2 | Descriptor = $aclObject 3 | } -------------------------------------------------------------------------------- /Chapter11/5.1.9_FileSystemRights.ps1: -------------------------------------------------------------------------------- 1 | [System.Security.AccessControl.FileSystemRights]1179817 2 | 3 | # Expects: 4 | # ReadAndExecute, Synchronize -------------------------------------------------------------------------------- /Chapter12/3.2.4_Select-Xml_FromFile.ps1: -------------------------------------------------------------------------------- 1 | Set-Content -Path file.xml -Value $string 2 | Select-Xml -Path file.xml -XPath '//car[colour="Green"]/engine' -------------------------------------------------------------------------------- /Chapter12/4.3.2_UsingXPath.ps1: -------------------------------------------------------------------------------- 1 | [System.Xml.XPath.Extensions]::XPathSelectElements( 2 | $xDocument, 3 | '//car[colour="Green"]/engine' 4 | ) -------------------------------------------------------------------------------- /Chapter15/3.6.4.ImportPSModule.ps1: -------------------------------------------------------------------------------- 1 | $initialSessionState = [InitialSessionState]::CreateDefault2() 2 | $initialSessionState.ImportPSModule('Pester') -------------------------------------------------------------------------------- /Chapter01/4.4.4_Update-Help_WrongCulture.ps1: -------------------------------------------------------------------------------- 1 | Update-Help -SourcePath C:\Temp -Module DnsClient 2 | 3 | # Expects error when culture is not en-US. 4 | -------------------------------------------------------------------------------- /Chapter05/8.2.6.UpdateOnly.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ Existing = "Old" } 2 | if ($hashtable.Contains("Existing")) { 3 | $hashtable.Existing = "New" 4 | } -------------------------------------------------------------------------------- /Chapter05/9.1.7.IndexOf.ps1: -------------------------------------------------------------------------------- 1 | $list.IndexOf('Harry', 2) # Start at index 2 2 | $list.IndexOf('Richard', 1, 2) # Start at index 1, and 2 elements -------------------------------------------------------------------------------- /Chapter07/1.1.3.GetAssemblyForType_Others.ps1: -------------------------------------------------------------------------------- 1 | [System.Management.Automation.PSCredential].Assembly 2 | [System.Management.Automation.PSObject].Assembly -------------------------------------------------------------------------------- /Chapter07/6.3.5.StaticMethodInvocation_ArgumentList_Failure.ps1: -------------------------------------------------------------------------------- 1 | $argumentList = "C:\none.exe", "bak" 2 | [System.IO.Path]::ChangeExtension($argumentList) -------------------------------------------------------------------------------- /Chapter08/1.2.2.Substring_StartAndLength.ps1: -------------------------------------------------------------------------------- 1 | $myString = 'abcdefghijklmnopqrstuvwxyz' 2 | $myString.Substring(3, 4) # Start at index 3, get 4 characters -------------------------------------------------------------------------------- /Chapter08/1.8.2.PadRight.ps1: -------------------------------------------------------------------------------- 1 | ('one', 'two', 'three').PadRight(10, '.') 2 | 3 | # Expects output: 4 | # 5 | # one....... 6 | # two....... 7 | # three..... -------------------------------------------------------------------------------- /Chapter08/1.8.3.PadLeft.ps1: -------------------------------------------------------------------------------- 1 | ('one', 'two', 'three').PadLeft(10, '.') 2 | 3 | # Expects output: 4 | # 5 | # .......one 6 | # .......two 7 | # .....three -------------------------------------------------------------------------------- /Chapter08/5.5.2.ComparingDates_RightHandSideCasting.ps1: -------------------------------------------------------------------------------- 1 | # Conversion of RHS will fail for en-GB culture. 2 | 3 | (Get-Date "13/01/2017") -gt "12/01/2017" -------------------------------------------------------------------------------- /Chapter10/5.4.6_Set-Acl_Fix.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\3 2 | $acl.SetAccessRuleProtection($false, $false) 3 | Set-Acl C:\Temp\ACL\3 -AclObject $acl -------------------------------------------------------------------------------- /Chapter12/3.7.2_ExampleXml.ps1: -------------------------------------------------------------------------------- 1 | [Xml]$xml = @" 2 | 3 | 4 | one 5 | two 6 | 7 | "@ -------------------------------------------------------------------------------- /Chapter14/1.5.2_Set-PSSessionConfiguration_ShowSecurityDescriptorUI.ps1: -------------------------------------------------------------------------------- 1 | Set-PSSessionConfiguration Microsoft.PowerShell -ShowSecurityDescriptorUI 2 | -------------------------------------------------------------------------------- /Chapter14/2.3.1_Invoke-Command.ps1: -------------------------------------------------------------------------------- 1 | $session = New-PSSession -ComputerName $env:COMPUTERNAME 2 | Invoke-Command { Get-Process -Id $PID } -Session $session -------------------------------------------------------------------------------- /Chapter14/2.3.8_InDisconnectedSession_ConnectAndReceive.ps1: -------------------------------------------------------------------------------- 1 | Get-PSSession -ComputerName PSTest | 2 | Connect-PSSession | 3 | Receive-PSSession -------------------------------------------------------------------------------- /Chapter14/2.7.1_Copy-Item_FromSession.ps1: -------------------------------------------------------------------------------- 1 | $session1 = New-PSSession PSTest 2 | Copy-Item -Path C:\temp\doc.txt -Destination C:\Temp -FromSession $session1 -------------------------------------------------------------------------------- /Chapter14/2.7.2_Copy-Item_ToSession.ps1: -------------------------------------------------------------------------------- 1 | $session2 = New-PSSession PSTest 2 | Copy-Item -Path C:\temp\doc.txt -Destination C:\Temp -ToSession $session2 -------------------------------------------------------------------------------- /Chapter16/1.5.3_line_break_on_logic_operator.ps1: -------------------------------------------------------------------------------- 1 | Get-Service | Where-Object { 2 | $_.Status -eq 'Running' -and 3 | $_.StartType -eq 'Manual' 4 | } -------------------------------------------------------------------------------- /Chapter16/3.5.3_Show-Verbose.ps1: -------------------------------------------------------------------------------- 1 | function Show-Verbose { 2 | [CmdletBinding()] 3 | param ( ) 4 | 5 | Write-Verbose 'Verbose message' 6 | } -------------------------------------------------------------------------------- /Chapter17/2.1.4.PSTypeNameAttribute_Add-Member.ps1: -------------------------------------------------------------------------------- 1 | $object = [PSCustomObject]@{ Property = 'Value' } 2 | $object | Add-Member -TypeName 'SomeTypeName' -------------------------------------------------------------------------------- /Chapter19/1.1.1.LocalMachine_v1.psm1: -------------------------------------------------------------------------------- 1 | # Must be imported as a module 2 | 3 | function Get-ComputerDescription { } 4 | function Set-ComputerDescription { } -------------------------------------------------------------------------------- /Chapter20/2.9.3_Mock_MockWith_Parameter.ps1: -------------------------------------------------------------------------------- 1 | Describe Subject { 2 | Mock Get-Date -MockWith { 3 | [DateTime]::new(2017, 1, 1) 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter03/1.3.1.StdOut.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Where-Object WorkingSet -gt 50MB 2 | 3 | # Expects process objects where the WorkingSet property is greater than 50MB -------------------------------------------------------------------------------- /Chapter04/1.2.7.JoinArray_Examples.ps1: -------------------------------------------------------------------------------- 1 | @(1, 2) + @(3, 4) 2 | (1, 2) + (3, 4) 3 | 1, 2 + 3, 4 4 | 5 | # All create a single array containing 1, 2, 3, and 4. -------------------------------------------------------------------------------- /Chapter04/3.4.1.LikeAndNotLike.ps1: -------------------------------------------------------------------------------- 1 | 'The cow jumped over the moon' -like '*moon*' 2 | 'Hello world' -like '??llo w*' 3 | '' -like '*' 4 | '' -notlike '?*' 5 | -------------------------------------------------------------------------------- /Chapter06/1.1.6.IfAndAssign_Practical.ps1: -------------------------------------------------------------------------------- 1 | if ($interface = Get-NetAdapter | Where-Object Status -eq 'Up') { 2 | Write-Host "$($interface.Name) is up" 3 | } -------------------------------------------------------------------------------- /Chapter08/1.6.1.Insert.ps1: -------------------------------------------------------------------------------- 1 | $string = 'The letter of the alphabet is a' 2 | $string.Insert(4, 'first ') # Insert this before "letter", include a trailing space -------------------------------------------------------------------------------- /Chapter08/2.1.1.ToBase64String.ps1: -------------------------------------------------------------------------------- 1 | [Byte[]]$bytes = 97, 98, 99, 100, 101 2 | [Convert]::ToBase64String($bytes) 3 | 4 | # Expects output: 5 | # 6 | # YWJjZGU= -------------------------------------------------------------------------------- /Chapter08/2.1.3.ToBase64String_CharArray.ps1: -------------------------------------------------------------------------------- 1 | [Convert]::ToBase64String('Hello world'.ToCharArray()) 2 | 3 | # Expects output: 4 | # 5 | # SGVsbG8gd29ybGQ= -------------------------------------------------------------------------------- /Chapter09/8.2.1.MatchIPAddress.ps1: -------------------------------------------------------------------------------- 1 | '1.2.3.4' -match '(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[ 0-9]{2}|2[0-4][0-9]|25[0-5])' -------------------------------------------------------------------------------- /Chapter10/1.3.1_New-PSDrive.ps1: -------------------------------------------------------------------------------- 1 | New-PSDrive X -PSProvider FileSystem -Root \\Server\Share 2 | New-PSDrive HKCR -PSProvider Registry -Root HKEY_CLASSES_ROO -------------------------------------------------------------------------------- /Chapter10/5.4.3_Get-Acl.ps1: -------------------------------------------------------------------------------- 1 | Get-Acl 2 | 2 | Select-Object -ExpandProperty Access | 3 | Select-Object FileSystemRights, IdentityReference, IsInherited -------------------------------------------------------------------------------- /Chapter10/5.6.1_Set-Acl_Setup_1.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\3 2 | $acl.SetAccessRuleProtection($true, $true) 3 | Set-Acl C:\Temp\ACL\3 -AclObject $acl -------------------------------------------------------------------------------- /Chapter10/5.6.2_Set-Acl_Setup_2.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\3 2 | $acl.SetAccessRuleProtection($false, $true) 3 | Set-Acl C:\Temp\ACL\3 -AclObject $acl -------------------------------------------------------------------------------- /Chapter11/4.1.1_Wmi_and_WmiClass.ps1: -------------------------------------------------------------------------------- 1 | [Wmi]"\\RemoteComputer\root\cimv2:Win32_Process.Handle=$PID" 2 | [WmiClass]"\\RemoteComputer\root\cimv2:Win32_Process" -------------------------------------------------------------------------------- /Chapter12/1.4.3_System.Web_HtmlDecode.ps1: -------------------------------------------------------------------------------- 1 | [System.Web.HttpUtility]::HtmlDecode("

Files > 100MB

") 2 | 3 | # Expects: 4 | #

Files > 100MB

-------------------------------------------------------------------------------- /Chapter14/2.3.5_AsJob.ps1: -------------------------------------------------------------------------------- 1 | $session = New-PSSession PSTest 2 | Invoke-Command -Session $session -AsJob -ScriptBlock { Start-Sleep -Seconds 120 'Done sleeping' } -------------------------------------------------------------------------------- /Chapter14/3.1.4_firewall-cmd.ps1: -------------------------------------------------------------------------------- 1 | # Run on Linux 2 | 3 | firewall-cmd --zone=home --change-interface=eth0 4 | firewall-cmd --zone=home --add-port=5986/tcp 5 | -------------------------------------------------------------------------------- /Chapter17/3.1.9.ValueFromPipeine_MutlipleParams_Trace-Command.ps1: -------------------------------------------------------------------------------- 1 | Trace-Command -Expression { 1 | Test-ValueFromPipeline } -PSHost -Name ParameterBinding 2 | -------------------------------------------------------------------------------- /Chapter19/3.2.3.ModuleService_InvokePrivateFunction.ps1: -------------------------------------------------------------------------------- 1 | # Must import ModuleService.psm1 2 | 3 | & (Get-Module ModuleService) { GetModuleServiceConnection } -------------------------------------------------------------------------------- /Chapter19/3.2.4.ModuleService_ListingCommands.ps1: -------------------------------------------------------------------------------- 1 | # Must import ModuleService.psm1 2 | 3 | & (Get-Module ModuleService) { Get-Command -Module ModuleService } -------------------------------------------------------------------------------- /Chapter20/2.9.12_GetParamBlock.ps1: -------------------------------------------------------------------------------- 1 | $command = Get-Command Add-DnsServerPrimaryZone 2 | [System.Management.Automation.ProxyCommand]::GetParamBlock($command) -------------------------------------------------------------------------------- /Chapter03/2.5.1.Add-Member_NoteProperty.ps1: -------------------------------------------------------------------------------- 1 | $empty = New-Object Object 2 | $empty | Add-Member -Name New -Value 'Hello world' -MemberType NoteProperty 3 | $empty -------------------------------------------------------------------------------- /Chapter03/7.3.1.Export-CliXml.ps1: -------------------------------------------------------------------------------- 1 | [PSCustomObject]@{ 2 | Number = 1 3 | Decimal = 2.3 4 | String = 'Hello world' 5 | } | Export-Clixml .\object.xml -------------------------------------------------------------------------------- /Chapter04/6.2.1.Or.ps1: -------------------------------------------------------------------------------- 1 | $true -or $true 2 | 2 -gt 1 -or "something" -ne "nothing" 3 | 1 -eq 1 -or 2 -eq 1 4 | (Test-Path C:\Windows) -or (Test-Path D:\Windows) 5 | -------------------------------------------------------------------------------- /Chapter05/6.1.4.ReferenceTypeAssignment_PropertyChange.ps1: -------------------------------------------------------------------------------- 1 | $object1.Name = 'New name' 2 | Write-Host $object2.Name 3 | 4 | # Expects output: 5 | # 6 | # New name -------------------------------------------------------------------------------- /Chapter06/1.2.2.Switch_Break.ps1: -------------------------------------------------------------------------------- 1 | $value = 1 2 | switch ($value) { 3 | 1 { Write-Host 'value is 1'; break } 4 | 1 { Write-Host 'value is still 1' } 5 | } -------------------------------------------------------------------------------- /Chapter06/2.3.1.do_until.ps1: -------------------------------------------------------------------------------- 1 | do { 2 | Write-Host "Waiting for boot" 3 | Start-Sleep -Seconds 5 4 | } until (Test-Connection 'SomeComputer' -Quiet -Count 1) -------------------------------------------------------------------------------- /Chapter06/2.5.1.break.ps1: -------------------------------------------------------------------------------- 1 | for ($i = 0; $i -lt 20; $i += 2) { 2 | Write-Host $i 3 | if ($i -eq 10) { 4 | break # Stop this loop 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter08/1.4.2.Replace_vs_-replace.ps1: -------------------------------------------------------------------------------- 1 | $string = 'Begin the begin.' 2 | $string -replace 'begin.', 'story, please.' 3 | $string.Replace('begin.', 'story, please.') -------------------------------------------------------------------------------- /Chapter08/3.1.2.ByteMultiplicationOperatorsAndFormat.ps1: -------------------------------------------------------------------------------- 1 | '{0:F} TB available' -f (123156235234522 / 1TB) 2 | 3 | # Expects output: 4 | # 5 | # 112.01 TB available -------------------------------------------------------------------------------- /Chapter10/2.1.3_Test-Path_If.ps1: -------------------------------------------------------------------------------- 1 | if (-not (Test-Path C:\Temp\NewDirectory -PathType Container)) { 2 | New-Item C:\Temp\NewDirectory -ItemType Directory 3 | } -------------------------------------------------------------------------------- /Chapter10/3.2.6_FileAttributes_assign.ps1: -------------------------------------------------------------------------------- 1 | $file = Get-Item 'somefile.txt' 2 | $file.Attributes = 'ReadOnly' 3 | $file.Attributes += 'ReadOnly' 4 | $file.Attributes -------------------------------------------------------------------------------- /Chapter11/2.1.1_WQL_SELECT.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -Query "SELECT * FROM Win32_Process" 2 | Get-CimInstance -Query "SELECT ProcessID, CommandLine FROM Win32_Process" -------------------------------------------------------------------------------- /Chapter11/3.1.4_Get-CimInstance_Query.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -Query "SELECT * FROM Win32_Process" 2 | Get-CimInstance -Query "SELECT Name, SID FROM Win32_UserAccount" -------------------------------------------------------------------------------- /Chapter12/1.3.2_ConvertTo-Html_Injecting_CSS.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | 2 | ConvertTo-Html -Property Name, Id, WorkingSet -Head $css | 3 | Set-Content report.html -------------------------------------------------------------------------------- /Chapter13/1.6.2_SecurityProtocol.ps1: -------------------------------------------------------------------------------- 1 | using namespace System.Net 2 | [ServicePointManager]::SecurityProtocol = [ServicePointManager]::SecurityProtocol -bor 'Tls12' -------------------------------------------------------------------------------- /Chapter13/2.3.5_GetAuthorizationCode.ps1: -------------------------------------------------------------------------------- 1 | $authorize = 'https://github.com/login/oauth/authorize?client_id={0}&scope={1}' -f 2 | $clientId, 3 | 'user:email' -------------------------------------------------------------------------------- /Chapter17/2.2.1.Exploration.ps1: -------------------------------------------------------------------------------- 1 | PS> [ValidateDrive]::new 2 | 3 | OverloadDefinitions 4 | ------------------- 5 | ValidateDrive new(Params string[] validRootDrives) -------------------------------------------------------------------------------- /Chapter18/2.3.1.constructors_default.ps1: -------------------------------------------------------------------------------- 1 | [MyClass]::new 2 | 3 | # Expects output: 4 | # 5 | # OverloadDefinitions 6 | # ------------------- 7 | # MyClass new() -------------------------------------------------------------------------------- /Chapter18/2.3.2.constructors_explicit.ps1: -------------------------------------------------------------------------------- 1 | class MyClass { 2 | [String]$Property 3 | MyClass() { 4 | $this.Property = 'Hello world' 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter18/3.1.4.DateTimeStringTransformationAttribute_result.ps1: -------------------------------------------------------------------------------- 1 | Test-Transform -Date '20190210090000' 2 | 3 | # Expects output: 4 | # 5 | # 10/02/2019 09:00:00 -------------------------------------------------------------------------------- /Chapter01/6.4.3_Confirm.ps1: -------------------------------------------------------------------------------- 1 | Set-Location $env:TEMP 2 | New-Item IMadeThisUp.txt -Force 3 | Remove-Item .\IMadeThisUp.txt -Confirm 4 | 5 | # Expects confirmation prompt -------------------------------------------------------------------------------- /Chapter02/2.1.2_Import-Module.ps1: -------------------------------------------------------------------------------- 1 | Import-Module -Name PSWorkflow 2 | Import-Module -Name C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSWorkflow\PSWorkflow.psd1 -------------------------------------------------------------------------------- /Chapter03/2.2.5.Property_with_spaces.ps1: -------------------------------------------------------------------------------- 1 | $object = [PSCustomObject]@{ 'Some Name' = 'Value' } 2 | $object."Some Name" 3 | $object.'Some Name' 4 | $object.{Some Name} -------------------------------------------------------------------------------- /Chapter06/1.1.1.If.ps1: -------------------------------------------------------------------------------- 1 | $value = 1 2 | if ($value -eq 1) { 3 | Write-Host 'value is 1' 4 | } elseif ($value -lt 10) { 5 | Write-Host 'value is less than 10' 6 | } -------------------------------------------------------------------------------- /Chapter06/2.3.2.do_while.ps1: -------------------------------------------------------------------------------- 1 | do { 2 | Write-Host "Waiting for shutdown" 3 | Start-Sleep -Seconds 5 4 | } while (Test-Connection 'SomeComputer' -Quiet -Count 1) -------------------------------------------------------------------------------- /Chapter07/6.3.6.StaticMethodInvocation_ArgumentList_InvokeMethod.ps1: -------------------------------------------------------------------------------- 1 | $argumentList = "C:\none.exe", "bak" 2 | [System.IO.Path]::ChangeExtension.Invoke($argumentList) -------------------------------------------------------------------------------- /Chapter07/8.1.4.TypeAccelerators_Adding.ps1: -------------------------------------------------------------------------------- 1 | $type = [PowerShell].Assembly.GetType('System.Management.Automation.TypeAccelerators') 2 | $type::Add('Accelerators', $type) -------------------------------------------------------------------------------- /Chapter08/1.3.2.Split_EmptyEntries.ps1: -------------------------------------------------------------------------------- 1 | $string = 'Surname,,GivenName' 2 | $array = $string.Split(',') 3 | $array.Count # This is 3 4 | $array[1] # This is empty -------------------------------------------------------------------------------- /Chapter10/5.4.1_Set-Acl_Protection_Enable.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\2 2 | $acl.SetAccessRuleProtection($true, $true) 3 | Set-Acl C:\Temp\ACL\2 -AclObject $acl -------------------------------------------------------------------------------- /Chapter10/5.4.2_Set-Acl_Protection_Disable.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\2 2 | $acl.SetAccessRuleProtection($false, $false) 3 | Set-Acl C:\Temp\ACL\2 -AclObject $acl -------------------------------------------------------------------------------- /Chapter10/5.4.4_Set-Acl_Protection_Discard.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\3 2 | $acl.SetAccessRuleProtection($true, $false) 3 | Set-Acl C:\Temp\ACL\3 -AclObject $acl -------------------------------------------------------------------------------- /Chapter12/1.4.2_System.Web_HtmlEncode.ps1: -------------------------------------------------------------------------------- 1 | '

{0}

' -f [System.Web.HttpUtility]::HtmlEncode('Files > 100MB') 2 | 3 | # Expects: 4 | #

Files > 100MB

-------------------------------------------------------------------------------- /Chapter15/3.2.1.BeginInvoke.ps1: -------------------------------------------------------------------------------- 1 | $psInstance = [PowerShell]::Create().AddCommand('Start-Sleep').AddParameter('Seconds', 300) 2 | $asyncResult = $psInstance.BeginInvoke() -------------------------------------------------------------------------------- /Chapter15/3.2.2.InvocationStateInfo.ps1: -------------------------------------------------------------------------------- 1 | $psInstance.InvocationStateInfo 2 | 3 | # Expects output: 4 | # 5 | # State Reason 6 | # ----- ------ 7 | # Running -------------------------------------------------------------------------------- /Chapter15/3.2.8.Stop.ps1: -------------------------------------------------------------------------------- 1 | $psInstance = [PowerShell]::Create() 2 | $psInstance.AddCommand('Start-Sleep').AddParameter('Seconds', 120).BeginInvoke() 3 | $psInstance.Stop() -------------------------------------------------------------------------------- /Chapter16/1.4.8_CBH_example_code_parser.ps1: -------------------------------------------------------------------------------- 1 | (Get-Help Get-Something -Examples).examples[0].example.code 2 | 3 | # Experts output: 4 | # 5 | # $something = Get-Something -------------------------------------------------------------------------------- /Chapter16/3.2.1_param_type.ps1: -------------------------------------------------------------------------------- 1 | # Will not run in the console as-is. Needs to be in a .ps1 file, or wrapped by function. 2 | param ( 3 | [String]$Parameter1 4 | ) -------------------------------------------------------------------------------- /Chapter16/4.1.4_WhatIf_running.ps1: -------------------------------------------------------------------------------- 1 | Test-ShouldProcess -WhatIf 2 | 3 | # Expects output: 4 | # 5 | # What if: Performing the operation "delete" on target "SomeObject". -------------------------------------------------------------------------------- /Chapter21/5.2.1_DemoFunction.ps1: -------------------------------------------------------------------------------- 1 | function Invoke-Something { 2 | [CmdletBinding()] 3 | param ( ) 4 | 5 | throw 'Error' 6 | Write-Host 'No error' 7 | } -------------------------------------------------------------------------------- /Chapter01/6.4.9_WhatIf.ps1: -------------------------------------------------------------------------------- 1 | Set-Location $env:TEMP 2 | New-Item IMadeThisUp.txt -Force 3 | Remove-Item .\IMadeThisUp.txt -WhatIf 4 | 5 | # Expects Confirm and WhatIf output -------------------------------------------------------------------------------- /Chapter03/1.2.1.StdOut.ps1: -------------------------------------------------------------------------------- 1 | $stdout = Get-CimInstance Win32_ComputerSystem -Verbose 2 | 3 | # Expects verbose output only 4 | 5 | $stdout 6 | 7 | # Expects normal output -------------------------------------------------------------------------------- /Chapter03/4.1.12.Select-Object_CalculatedProperties.ps1: -------------------------------------------------------------------------------- 1 | Get-Process | Select-Object -Property Name, Id, 2 | @{Name='FileOwner'; Expression={ (Get-Acl $_.Path).Owner }} -------------------------------------------------------------------------------- /Chapter04/3.5.1.GreaterThanAndLessThan.ps1: -------------------------------------------------------------------------------- 1 | 1 -ge 1 # Returns true 2 | 2 -gt 1 # Returns true 3 | 1.4 -lt 1.9 # Returns true 4 | 1.1 -le 1.1 # Returns true -------------------------------------------------------------------------------- /Chapter05/7.6.9.RemovingElements_UsingLoop.ps1: -------------------------------------------------------------------------------- 1 | $newArray = for ($i = 0; $i -lt $oldArray.Count; $i++) { 2 | if ($i -ne 49) { 3 | $oldArray[$i] 4 | } 5 | } -------------------------------------------------------------------------------- /Chapter05/8.2.4.ImplicitAdd.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ Existing = "Old" } 2 | $hashtable["New"] = "New" # Add this 3 | $hashtable["Existing"] = "Updated" # Update this -------------------------------------------------------------------------------- /Chapter06/1.2.3.Switch_Default.ps1: -------------------------------------------------------------------------------- 1 | $value = 2 2 | switch ($value) { 3 | 1 { Write-Host 'value is 1' } 4 | default { Write-Host 'No conditions matched' } 5 | } -------------------------------------------------------------------------------- /Chapter08/1.3.4.Split_RemoveEmptyEntries_Assign.ps1: -------------------------------------------------------------------------------- 1 | $string = 'Surname,,GivenName' 2 | $surname, $givenName = $string.Split(',', [StringSplitOptions]::RemoveEmptyEntries) -------------------------------------------------------------------------------- /Chapter08/2.2.5.Export-CsvAndImport-Csv.ps1: -------------------------------------------------------------------------------- 1 | Get-Process -Id $pid | 2 | Select-Object Name, Id, Path | 3 | Export-Csv 'somefile.csv' 4 | Import-Csv somefile.csv 5 | -------------------------------------------------------------------------------- /Chapter08/5.1.2.Test-DateTime.ps1: -------------------------------------------------------------------------------- 1 | function Test-DateTime { 2 | param ( 3 | [DateTime]$Date 4 | ) 5 | 6 | $Date 7 | } 8 | Test-DateTime -Date "11/10/2000" -------------------------------------------------------------------------------- /Chapter08/5.2.1.DateTime_ParseExact.ps1: -------------------------------------------------------------------------------- 1 | $string = '20170102-2030' # Represents 1st February 2017, 20:30 2 | [DateTime]::ParseExact($string, 'yyyyddMM-HHmm', (Get-Culture)) -------------------------------------------------------------------------------- /Chapter09/4.1.3.Range_IsASCII.ps1: -------------------------------------------------------------------------------- 1 | '"#' -match '[!-9]+' 2 | 3 | # Expects output: 4 | # 5 | # True 6 | 7 | $matches[0] 8 | 9 | # Expects output: 10 | # 11 | # "# -------------------------------------------------------------------------------- /Chapter11/3.1.1_Get-CimInstance.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance -ClassName Win32_OperatingSystem 2 | Get-CimInstance -ClassName Win32_Service 3 | Get-CimInstance -ClassName Win32_Share -------------------------------------------------------------------------------- /Chapter11/5.1.11_CreateTrustee.ps1: -------------------------------------------------------------------------------- 1 | $trustee = New-CimInstance (Get-CimClass Win32_Trustee) -ClientOnly 2 | $trustee.Domain = $env:USERDOMAIN 3 | $trustee.Name = $env:USERNAME -------------------------------------------------------------------------------- /Chapter12/3.9.3_OuterXml.ps1: -------------------------------------------------------------------------------- 1 | $xml.OuterXml 2 | 3 | # Expects: 4 | # 1234 -------------------------------------------------------------------------------- /Chapter13/2.3.10_UseToken.ps1: -------------------------------------------------------------------------------- 1 | $headers = @{ 2 | Authorization = 'token {0}' -f $token 3 | } 4 | Invoke-RestMethod 'https://api.github.com/user/emails' -Headers $headers -------------------------------------------------------------------------------- /Chapter13/3.2.4_SimpleArguments.ps1: -------------------------------------------------------------------------------- 1 | $service.GetAtomicNumber('oxygen') 2 | # Expects: 3 | # 4 | # 8 5 | 6 | $service.GetAtomicMass('H') 7 | # Expects: 8 | # 9 | # 1.00794(4) -------------------------------------------------------------------------------- /Chapter14/4.2.5_GetPublicKey.ps1: -------------------------------------------------------------------------------- 1 | # Run on Linux. Assumes ssh-keygen was used to create key pair, or that a key pair already exists. 2 | 3 | Get-Content ~/.ssh/id_rsa.pub -------------------------------------------------------------------------------- /Chapter15/3.3.1.EndInvoke.ps1: -------------------------------------------------------------------------------- 1 | $psInstance = [PowerShell]::Create() 2 | $asyncResult = $psInstance.AddScript('1..10').BeginInvoke() 3 | $psInstance.EndInvoke($asyncResult) -------------------------------------------------------------------------------- /Chapter15/3.6.5.ImportPSModule_Multiple.ps1: -------------------------------------------------------------------------------- 1 | $initialSessionState.ImportPSModule(@( 2 | 'NetAdapter' 3 | @{ ModuleName = 'Pester'; ModuleVersion = '4.6.0' } 4 | )) -------------------------------------------------------------------------------- /Chapter16/3.1.1_param.ps1: -------------------------------------------------------------------------------- 1 | # Will not run in the console as-is. Needs to be in a .ps1 file, or wrapped by function. 2 | param ( 3 | $Parameter1, 4 | $Parameter2 5 | ) -------------------------------------------------------------------------------- /Chapter17/2.4.1.PSReference.ps1: -------------------------------------------------------------------------------- 1 | function Set-Value { 2 | [CmdletBinding()] 3 | param ( 4 | [PSObject]$Object 5 | ) 6 | 7 | $Object.Value = 2 8 | } -------------------------------------------------------------------------------- /Chapter17/2.4.2.PSReference_ReferenceTypes.ps1: -------------------------------------------------------------------------------- 1 | $myObject = [PSCustomObject]@{ Value = 1 } 2 | Set-Value $myObject 3 | $myObject.Value 4 | 5 | # Expects output: 6 | # 7 | # 2 -------------------------------------------------------------------------------- /Chapter17/3.1.12.ValueFromPipeine_PSTypeName_CreatingTheType.ps1: -------------------------------------------------------------------------------- 1 | [PSCustomObject]@{ 2 | Name = 'Value' 3 | PSTypeName = 'CustomTypeName' 4 | } | Get-InputObject -------------------------------------------------------------------------------- /Chapter18/1.3.3.enum_sequencing_skipping_and_restarting.ps1: -------------------------------------------------------------------------------- 1 | enum MyEnum { 2 | One = 1 3 | Two 4 | Five = 5 5 | Six 6 | First = 1 7 | Second 8 | } -------------------------------------------------------------------------------- /Chapter19/2.2.1.ModuleBuilder.ps1: -------------------------------------------------------------------------------- 1 | Install-Module ModuleBuilder -Scope CurrentUser 2 | 3 | Build-Module -SourcePath C:\Workspace\LocalMachine\LocalMachine\LocalMachine.psd1 -------------------------------------------------------------------------------- /Chapter21/5.5.1_Trap.ps1: -------------------------------------------------------------------------------- 1 | trap [InvalidOperationException] { 2 | Write-Host 'An invalid operation' 3 | } 4 | trap { 5 | Write-Host 'Catch all other exceptions' 6 | } -------------------------------------------------------------------------------- /Chapter01/6.3.2_ParameterSetSelection.ps1: -------------------------------------------------------------------------------- 1 | $process = Start-Process notepad -PassThru 2 | Stop-Process -InputObject $process 3 | Stop-Process $process 4 | $process | Stop-Process -------------------------------------------------------------------------------- /Chapter04/3.5.3.GreaterThanAndLessThan_Alphabetical_CaseSensitive.ps1: -------------------------------------------------------------------------------- 1 | 'bears' -gt 'Bears' # False, they are equal to one another 2 | 'bears' -clt 'Bears' # True, b before B -------------------------------------------------------------------------------- /Chapter04/6.1.1.And.ps1: -------------------------------------------------------------------------------- 1 | $true -and $true 2 | 1 -lt 2 -and "string" -like 's*' 3 | 1 -eq 1 -and 2 -eq 2 -and 3 -eq 3 4 | (Test-Path C:\Windows) -and (Test-Path 'C:\Program Files') -------------------------------------------------------------------------------- /Chapter09/7.1.3.CaptureGroups_Tokens.ps1: -------------------------------------------------------------------------------- 1 | 'first second third' -replace '(first) ((second) (third))', '$1, $4, $2' 2 | 3 | # Expects output: 4 | # 5 | # first, third, second third -------------------------------------------------------------------------------- /Chapter10/5.10.1_GenericAccessRights.ps1: -------------------------------------------------------------------------------- 1 | foreach ($value in -536805376, 268435456) { 2 | '{0,-10}: {1}' -f $value, [Convert]::ToString($value, 2).PadLeft(32, '0') 3 | } 4 | -------------------------------------------------------------------------------- /Chapter10/5.2.2_Set-Acl_Owner.ps1: -------------------------------------------------------------------------------- 1 | $acl = Get-Acl C:\Temp\ACL\1 2 | $acl.SetOwner([System.Security.Principal.NTAccount]'Administrator') 3 | Set-Acl C:\Temp\ACL\1 -AclObject $acl -------------------------------------------------------------------------------- /Chapter14/2.5.1_Import-PSSession.ps1: -------------------------------------------------------------------------------- 1 | $computerName = 'PSTest' 2 | $session = New-PSSession -ComputerName $computerName 3 | Import-PSSession -Session $session -Module NetAdapter -------------------------------------------------------------------------------- /Chapter16/4.1.2_WhatIf_running.ps1: -------------------------------------------------------------------------------- 1 | Test-ShouldProcess -WhatIf 2 | 3 | # Expects output: 4 | # 5 | # What if: Performing the operation "Test-ShouldProcess" on target "SomeObject". -------------------------------------------------------------------------------- /Chapter17/2.1.6.PSTypeNameAttribute_Exploration.ps1: -------------------------------------------------------------------------------- 1 | (Get-Command Set-NetAdapter).Parameters['InputObject'].Attributes | 2 | Where-Object TypeId -eq ([PSTypeNameAttribute]) -------------------------------------------------------------------------------- /Chapter18/1.6.1.enum_union.ps1: -------------------------------------------------------------------------------- 1 | enum AliasName { 2 | OS 3 | Process 4 | } 5 | 6 | enum ClassName { 7 | Win32_OperatingSystem 8 | Win32_Process 9 | } 10 | -------------------------------------------------------------------------------- /Chapter18/2.5.1.inheritance.ps1: -------------------------------------------------------------------------------- 1 | class MyBaseClass { 2 | [String]$BaseProperty = 'baseValue' 3 | } 4 | class MyClass : MyBaseClass { 5 | [String]$Property = 'Value' 6 | } -------------------------------------------------------------------------------- /Chapter18/2.5.2.inherited_members.ps1: -------------------------------------------------------------------------------- 1 | [MyClass]::new() 2 | 3 | # Expects output: 4 | # 5 | # Property BaseProperty 6 | # -------- ------------ 7 | # Value baseValue -------------------------------------------------------------------------------- /Chapter18/2.7.1.static.ps1: -------------------------------------------------------------------------------- 1 | class MyClass { 2 | static [String] $Property = 'Hello world' 3 | static [String] Method() { 4 | return 'Hello world' 5 | } 6 | } -------------------------------------------------------------------------------- /Chapter21/3.2.3_Write-Error_Loop.ps1: -------------------------------------------------------------------------------- 1 | function Test-Error { 2 | for ($i = 0; $i -lt 5; $i++) { 3 | Write-Error -Message "Iteration: $i" 4 | } 5 | } 6 | Test-Error -------------------------------------------------------------------------------- /Chapter21/5.1.3_Cmdlet_Exceptions.ps1: -------------------------------------------------------------------------------- 1 | function caller { 2 | ConvertFrom-Json -InputObject '{{' 3 | child1 4 | } 5 | function child1 { 6 | 'Called' 7 | } 8 | caller -------------------------------------------------------------------------------- /Chapter03/5.1.7.Group-Object_AsHashtable.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = 'one', 'two', 'two' | Group-Object -AsHashtable -AsString 2 | $hashtable['one'] 3 | 4 | # Expects output: 5 | # 6 | # one -------------------------------------------------------------------------------- /Chapter05/1.1.4.FileSystemProvider.ps1: -------------------------------------------------------------------------------- 1 | ${C:\Windows\Temp\variable.txt} = "New value" 2 | 3 | Get-Content C:\Windows\Temp\variable.txt 4 | 5 | # Expects output: 6 | # 7 | # New value -------------------------------------------------------------------------------- /Chapter07/6.3.3.StaticMethodInvocation_FromVariable.ps1: -------------------------------------------------------------------------------- 1 | $type = [System.Net.NetworkInformation.NetworkInterface] 2 | $methodName = 'GetAllNetworkInterfaces' 3 | $type::$methodName() -------------------------------------------------------------------------------- /Chapter08/1.3.3.Split_RemoveEmptyEntries.ps1: -------------------------------------------------------------------------------- 1 | $string = 'Surname,,GivenName' 2 | $array = $string.Split(',', [StringSplitOptions]::RemoveEmptyEntries) 3 | $array.Count # This is 2 -------------------------------------------------------------------------------- /Chapter08/2.3.3.Convert-String_Hashtable.ps1: -------------------------------------------------------------------------------- 1 | 'Michael Caine', 'Benny Hill', 'Raf Vallone' | Convert-String -Example @{ 2 | Before = 'First Second' 3 | After = 'FSecond' 4 | } -------------------------------------------------------------------------------- /Chapter08/5.1.1.CastToDateTime.ps1: -------------------------------------------------------------------------------- 1 | # Note: en-GB vs en-US culture behaviour 2 | 3 | $string = "11/10/2000" # 11th October 2000 4 | [DateTime]$string # 10th November 2000 -------------------------------------------------------------------------------- /Chapter09/8.2.2.UseIPAddressTryParse.ps1: -------------------------------------------------------------------------------- 1 | $ipAddress = [IPAddress]0 # Used as a placeholder 2 | if ([IPAddress]::TryParse("::1", [ref]$ipAddress)) { 3 | $ipAddress 4 | } 5 | -------------------------------------------------------------------------------- /Chapter10/2.2.1_New-Item.ps1: -------------------------------------------------------------------------------- 1 | New-Item $env:Temp\newfile.txt -ItemType File 2 | New-Item $env:Temp\newdirectory -ItemType Directory 3 | New-Item HKLM:\Software\NewKey -ItemType Key -------------------------------------------------------------------------------- /Chapter13/2.3.1_BasicAuthentication.ps1: -------------------------------------------------------------------------------- 1 | $params = @{ 2 | Uri = 'https://api.github.com/user/emails' 3 | Credential = Get-Credential 4 | } 5 | Invoke-RestMethod @params -------------------------------------------------------------------------------- /Chapter13/3.1.2_New-WebServiceProxy.ps1: -------------------------------------------------------------------------------- 1 | $params = @{ 2 | Uri = 'http://localhost:62369/Service.asmx' 3 | Namespace = 'SOAP' 4 | } 5 | $service = New-WebServiceProxy @params -------------------------------------------------------------------------------- /Chapter16/3.5.5_CmdletBinding_positional_binding.ps1: -------------------------------------------------------------------------------- 1 | function Test-Binding { 2 | [CmdletBinding(PositionalBinding = $false)] 3 | param ( 4 | $Parameter1 5 | ) 6 | } -------------------------------------------------------------------------------- /Chapter17/1.4.2.ValueFromRemainingArguments_UnboundArguments.ps1: -------------------------------------------------------------------------------- 1 | Test-BasicBinding -Parameter1 value1 -Parameter2 value2 2 | 3 | # Expects output: 4 | # 5 | # -Parameter2 6 | # Value2 -------------------------------------------------------------------------------- /Chapter17/2.2.2.ValidateNotNull.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateNotNull { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateNotNull()] 5 | $Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter19/4.2.2.ShowGlobalVariable.ps1: -------------------------------------------------------------------------------- 1 | Get-Variable VariableName 2 | 3 | # Expects output: 4 | # 5 | # Name Value 6 | # ---- ----- 7 | # VariableName Value -------------------------------------------------------------------------------- /Chapter21/3.1.5_UsingTargetObject.ps1: -------------------------------------------------------------------------------- 1 | $Error[0].TargetObject 2 | 3 | # Expects output: 4 | # Numerator Denominator 5 | # --------- ----------- 6 | # 10 0 -------------------------------------------------------------------------------- /Chapter21/5.1.4_Method_Exceptions.ps1: -------------------------------------------------------------------------------- 1 | function caller { 2 | [IPAddress]::Parse('this is not an IP') 3 | child1 4 | } 5 | function child1 { 6 | 'Called' 7 | } 8 | caller -------------------------------------------------------------------------------- /Chapter05/9.1.5.AddRange_And_Index.ps1: -------------------------------------------------------------------------------- 1 | $list = New-Object System.Collections.Generic.List[String] 2 | $list.AddRange([String[]]("Tom", "Richard", "Harry")) 3 | $list[1] # Returns Richard -------------------------------------------------------------------------------- /Chapter06/3.1.3.foreach_goodpractice.ps1: -------------------------------------------------------------------------------- 1 | $serviceProcesses = foreach ($service in Get-CimInstance Win32_Service Filter 'State="Running"') { 2 | Get-Process -Id $service.ProcessId 3 | } -------------------------------------------------------------------------------- /Chapter07/5.2.6.ContructorSelection.ps1: -------------------------------------------------------------------------------- 1 | $stringBuilder = New-Object System.Text.StringBuilder('Hello world') 2 | $stringBuilder.ToString() 3 | 4 | # Expects output: 5 | # 6 | # Hello world -------------------------------------------------------------------------------- /Chapter08/1.7.1.IndexOfAndLastIndexOf.ps1: -------------------------------------------------------------------------------- 1 | $string = 'abcdefedcba' 2 | $string.IndexOf('b') # Returns 1 3 | $string.LastIndexOf('b') # Returns 9 4 | $string.IndexOf('ed') # Returns 6 -------------------------------------------------------------------------------- /Chapter08/2.2.3.ConvertFrom-Csv_MultilineField.ps1: -------------------------------------------------------------------------------- 1 | 'David,0123456789,28,"1 Some street, 2 | A Lane"' | 3 | ConvertFrom-Csv -Header Name, Phone, Age, Address | 4 | Format-Table -Wrap -------------------------------------------------------------------------------- /Chapter09/7.3.3.NonCapturingGroup_Replace.ps1: -------------------------------------------------------------------------------- 1 | 'first second third' -replace '(first) (?:(second) (third))', '$1, $2, $3' 2 | 3 | # Expects output: 4 | # 5 | # first, second, third 6 | -------------------------------------------------------------------------------- /Chapter10/5.9.2_AddAccessRule.ps1: -------------------------------------------------------------------------------- 1 | $key = New-Item HKCU:\TestKey -ItemType Key -Force 2 | $acl = Get-Acl $key.PSPath 3 | $acl.AddAccessRule($ace) 4 | Set-Acl $key.PSPath -AclObject $acl -------------------------------------------------------------------------------- /Chapter14/5.2.1_PassingCredentials.ps1: -------------------------------------------------------------------------------- 1 | $Credential = Get-Credential 2 | Invoke-Command -ComputerName PSTEST -ScriptBlock { 3 | Get-ADUser -Filter * -Credential $using:Credential 4 | } -------------------------------------------------------------------------------- /Chapter15/3.1.5.AddScript.ps1: -------------------------------------------------------------------------------- 1 | $script = @' 2 | Get-Process -Name powershell 3 | Get-Service | Select-Object -First 1 4 | '@ 5 | [PowerShell]::Create().AddScript($script).Invoke() -------------------------------------------------------------------------------- /Chapter15/3.2.7.OrphanedRunspace_InProgress.ps1: -------------------------------------------------------------------------------- 1 | # Expects output from 3.2.6 to continue to fill the file, even when both variables are null. 2 | 3 | Get-Content c:\temp\output.txt -Wait -------------------------------------------------------------------------------- /Chapter15/3.6.6.ImportPSSnapIn.ps1: -------------------------------------------------------------------------------- 1 | $warning = [System.Management.Automation.Runspaces.PSSnapInException]::new() 2 | $initialSessionState.ImportPSSnapIn('WDeploySnapin3.0', [Ref]$warning) -------------------------------------------------------------------------------- /Chapter16/3.3.1_Default_value.ps1: -------------------------------------------------------------------------------- 1 | # Will not run in the console as-is. Needs to be in a .ps1 file, or wrapped by function. 2 | param ( 3 | [String]$Parameter1 = 'DefaultValue' 4 | ) -------------------------------------------------------------------------------- /Chapter17/2.2.15.ValidateRange.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateRange { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateRange(1, 20)] 5 | [Int]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.6.ValidateCount.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateCount { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateCount(1, 1)] 5 | [String[]]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.7.ValidateDrive.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateDrive { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateDrive('C')] 5 | [String]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter03/2.2.6.Property_with_variable.ps1: -------------------------------------------------------------------------------- 1 | $object = [PSCustomObject]@{ 'Some Name' = 'Value' } 2 | $propertyName = 'Some Name' 3 | $object.$propertyName 4 | 5 | # Expects: 6 | # 7 | # Value -------------------------------------------------------------------------------- /Chapter03/7.2.4.ConvertFrom-Csv.ps1: -------------------------------------------------------------------------------- 1 | "powershell,404" | ConvertFrom-Csv -Header Name, Id 2 | 3 | # Expects output: 4 | # 5 | # Name Id 6 | # ---- -- 7 | # powershell 404 -------------------------------------------------------------------------------- /Chapter05/8.2.5.ImplicitAdd_PropertyNotation.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ Existing = "Old" } 2 | $hashtable.New = "New" # Add this 3 | $hashtable.Existing = "Updated" # Update this -------------------------------------------------------------------------------- /Chapter07/9.2.4.UsingMultipleNamespaces.ps1: -------------------------------------------------------------------------------- 1 | using namespace System.IO; using namespace System.Text 2 | 3 | [File].FullName System.IO.File 4 | [StringBuilder].FullName System.Text.StringBuilder -------------------------------------------------------------------------------- /Chapter12/3.9.2_ImportNode.ps1: -------------------------------------------------------------------------------- 1 | foreach ($node in $newNodes.SelectNodes('/root/name')) { 2 | $newNode = $xml.ImportNode($node, $true) 3 | $null = $xml.list.AppendChild($newNode) 4 | } -------------------------------------------------------------------------------- /Chapter12/4.4.2_ToString.ps1: -------------------------------------------------------------------------------- 1 | $xDocument.ToString() 2 | 3 | # Expects: 4 | # 5 | # 6 | # 1 7 | # 2 8 | # 3 9 | # -------------------------------------------------------------------------------- /Chapter13/2.1.2_Invoke-RestMethod_UsingBody.ps1: -------------------------------------------------------------------------------- 1 | Invoke-RestMethod -Uri https://api.github.com/search/code -Body @{ 2 | q = 'Get-Content language:powershell repo:powershell/powershell' 3 | } -------------------------------------------------------------------------------- /Chapter13/2.3.4_OAUTH_Secrets.ps1: -------------------------------------------------------------------------------- 1 | # These values must be replaced with values taken from GitHub when creating an application. 2 | 3 | $clientId = 'FromGitHub' 4 | $clientSecret = 'FromGitHub' -------------------------------------------------------------------------------- /Chapter14/4.2.2_InstallSSHD.ps1: -------------------------------------------------------------------------------- 1 | # Run on Windows. Assumes openssh was previously installed using chocolatey. 2 | 3 | & "C:\Program Files\OpenSSH-Win64\install-sshd.ps1" 4 | Start-Service sshd -------------------------------------------------------------------------------- /Chapter17/1.1.2.ParameterAttributeAndCmdletBinding.ps1: -------------------------------------------------------------------------------- 1 | [CmdletBinding()] 2 | param ( 3 | [Parameter()] 4 | $Paramter1 5 | ) 6 | 7 | # Must be run as a script or enclosed in a function. -------------------------------------------------------------------------------- /Chapter17/1.4.1.ValueFromRemainingArguments_BasicFunction.ps1: -------------------------------------------------------------------------------- 1 | function Test-BasicBinding { 2 | param ( 3 | $Parameter1 4 | ) 5 | 6 | $MyInvocation.UnboundArguments 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.19.ValidateSet.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateSet { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateSet('One', 'Two', 'Three')] 5 | [String]$Value 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.8.ValidateLength.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateLength { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateLength(2, 6)] 5 | [String[]]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/3.1.8.ValueFromPipeine_MutlipleParams_Usage.ps1: -------------------------------------------------------------------------------- 1 | 1..2 | Test-ValueFromPipeline 2 | 3 | # Expects output: 4 | # 5 | # Parameter1: 1 :: Parameter2: 1 6 | # Parameter1: 2 :: Parameter2: 2 -------------------------------------------------------------------------------- /Chapter21/4.2.3_Re-throw_ErrorVariable.ps1: -------------------------------------------------------------------------------- 1 | try { 2 | throw 'Error' 3 | } catch { 4 | Write-Error -Exception $_.Exception -ErrorId 'GeneratedErrorId' -Category 'InvalidOperation' 5 | } -------------------------------------------------------------------------------- /Chapter21/5.5.2_Trap_Terminating.ps1: -------------------------------------------------------------------------------- 1 | & { 2 | Write-Host 'Statement1' 3 | throw 'Statement2' 4 | Write-Host 'Statement3' 5 | 6 | trap { Write-Host 'An error occurred' } 7 | } -------------------------------------------------------------------------------- /Chapter01/6.5.14_UsingForce.ps1: -------------------------------------------------------------------------------- 1 | Set-Location $env:TEMP 2 | New-Item IMadeThisUp.txt -Force 3 | Set-ItemProperty .\IMadeThisUp.txt –Name Attributes –Value Hidden 4 | Remove-Item IMadeThisUp.txt -Force -------------------------------------------------------------------------------- /Chapter03/7.1.3.Export-Csv_Append.ps1: -------------------------------------------------------------------------------- 1 | Get-Process powershell | Select-Object Name | Export-Csv .\Processes.csv 2 | Get-Process explorer | Select-Object Name, Id | Export-Csv .\Processes.csv -Append -------------------------------------------------------------------------------- /Chapter04/3.5.2.GreaterThanAndLessThan_Alphabetical_CaseInsensitive.ps1: -------------------------------------------------------------------------------- 1 | 'apples' -lt 'pears' # Returns true 2 | 'Apples' -lt 'pears' # Returns true 3 | 'bears' -gt 'Apples' # Returns true -------------------------------------------------------------------------------- /Chapter07/5.2.9.ArgumentsAsAnArray.ps1: -------------------------------------------------------------------------------- 1 | $params = @{ 2 | TypeName = 'System.Text.StringBuilder' 3 | ArgumentList = 'Initial value', 50 4 | } 5 | 6 | $stringBuilder = New-Object @params -------------------------------------------------------------------------------- /Chapter08/1.5.3.Trim_Overloads.ps1: -------------------------------------------------------------------------------- 1 | 'string'.Trim 2 | 3 | # Expects output: 4 | # 5 | # OverloadDefinitions 6 | # ------------------- 7 | # string Trim(Params char[] trimChars) 8 | # string Trim() -------------------------------------------------------------------------------- /Chapter08/2.1.2.ToBase64String_ASCII.ps1: -------------------------------------------------------------------------------- 1 | $bytes = [System.Text.Encoding]::ASCII.GetBytes('Hello world') 2 | [Convert]::ToBase64String($bytes) 3 | 4 | # Expects output: 5 | # 6 | # SGVsbG8gd29ybGQ= -------------------------------------------------------------------------------- /Chapter08/4.0.1.Casting.ps1: -------------------------------------------------------------------------------- 1 | [Int]"2" # String to Int32 2 | [Decimal]"3.141" # String to Decimal 3 | [UInt32]10 # Int32 to UInt32 4 | [SByte]-5 # Int32 to SByte -------------------------------------------------------------------------------- /Chapter09/3.1.2.LazyQuanifier.ps1: -------------------------------------------------------------------------------- 1 | 'C:\long\path\to\some\files' -match '.*?\\' 2 | 3 | # Expects output: 4 | # 5 | # True 6 | 7 | $matches[0] 8 | 9 | # Expects output: 10 | # 11 | # C:\ -------------------------------------------------------------------------------- /Chapter09/4.1.6.Range_SimpleIPAddress.ps1: -------------------------------------------------------------------------------- 1 | (ipconfig) -match 'IPv4 Address.+: *[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' 2 | 3 | # Expects output: 4 | # 5 | # IPv4 Address. . . . . . . . . . . : 172.16.255.30 -------------------------------------------------------------------------------- /Chapter10/4.1.1_Get-ItemProperty.ps1: -------------------------------------------------------------------------------- 1 | Get-ItemProperty -Path HKCU:\Environment 2 | Get-ItemProperty -Path HKCU:\Environment -Name Path 3 | Get-ItemProperty -Path HKCU:\Environment -Name Path, Temp -------------------------------------------------------------------------------- /Chapter13/1.4.2_Invoke-WebRequest_CertificatePolicy.ps1: -------------------------------------------------------------------------------- 1 | Invoke-WebRequest "https://expired.badssl.com/" 2 | 3 | # Expects: 4 | # 5 | # StatusCode : 200 6 | # StatusDescription : OK 7 | # ... -------------------------------------------------------------------------------- /Chapter14/4.2.6_AddToAuthorizedKeys.ps1: -------------------------------------------------------------------------------- 1 | # Run on Windows. Paste public key copied from Linux. 2 | 3 | $publicKey = 'ssh-rsa AAAABG...' 4 | Set-Content -Path ~/.ssh/authorized_keys -Value $publicKey -------------------------------------------------------------------------------- /Chapter15/3.7.1.InitialSessionState_and_RunspacePool.ps1: -------------------------------------------------------------------------------- 1 | $initialSessionState = [InitialSessionState]::CreateDefault2() 2 | $runspacePool = [RunspaceFactory]::CreateRunspacePool($initialSessionState) -------------------------------------------------------------------------------- /Chapter16/2.2.2_Show-Pipeline_running.ps1: -------------------------------------------------------------------------------- 1 | $result = 1..2 | Show-Pipeline 2 | 3 | # Expects output: 4 | # 5 | # Pipeline position 1: Start 6 | # Pipeline position 1: 1 7 | # Pipeline position 1: 2 -------------------------------------------------------------------------------- /Chapter17/1.1.4.ParameterAttribute_BooleanShorthand.ps1: -------------------------------------------------------------------------------- 1 | function Test-Mandatory { 2 | [CmdletBinding()] 3 | param ( 4 | [Parameter(Mandatory)] 5 | $Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.9.ValidatePattern.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidatePattern { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidatePattern('^Hello')] 5 | [String]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter18/2.5.3.inherited_override.ps1: -------------------------------------------------------------------------------- 1 | class MyBaseClass { 2 | [String] ToString() { return 'default' } 3 | } 4 | class MyClass : MyBaseClass { 5 | [String] ToString() { return 'new' } 6 | } -------------------------------------------------------------------------------- /Chapter18/5.5.4.Dsc_ComputerDescription_Get.ps1: -------------------------------------------------------------------------------- 1 | $class.Get() 2 | 3 | # Expects output similar to: 4 | # 5 | # Ensure Description 6 | # ------ ----------- 7 | # Absent Computer description -------------------------------------------------------------------------------- /Chapter19/3.2.2.ModuleService_AccessingVariables.ps1: -------------------------------------------------------------------------------- 1 | # Must import ModuleService.psm1 2 | 3 | & (Get-Module ModuleService) { $connection } 4 | 5 | # Expected output: 6 | # 7 | # DefaultConnection -------------------------------------------------------------------------------- /Chapter03/3.2.3.ForEach-Object_MemberInvocation.ps1: -------------------------------------------------------------------------------- 1 | (Get-Date '01/01/2019'), (Get-Date '01/01/2020') | ForEach-Object ToString('yyyyMMdd') 2 | 3 | # Expects output: 4 | # 5 | # 20190101 6 | # 20200101 -------------------------------------------------------------------------------- /Chapter03/4.1.7.Select-Object_ExpandProperty.ps1: -------------------------------------------------------------------------------- 1 | Get-ChildItem C:\ | Select-Object -ExpandProperty FullName 2 | Get-ChildItem $env:SYSTEMROOT\*.dll | Select-Object Name, Length -ExpandProperty VersionInfo -------------------------------------------------------------------------------- /Chapter03/5.2.1.Measure-Object.ps1: -------------------------------------------------------------------------------- 1 | 1, 5, 9, 79 | Measure-Object 2 | 3 | # Expects output: 4 | # Count : 4 5 | # Average : 6 | # Sum : 7 | # Maximum : 8 | # Minimum : 9 | # Property : -------------------------------------------------------------------------------- /Chapter04/3.2.3.ArrayEqualsNull_InvalidComparison.ps1: -------------------------------------------------------------------------------- 1 | $array = 1, 2, $null, $null 2 | if ($array -eq $null) { Write-Host 'No values in array' } 3 | 4 | # Expects output: 5 | # 6 | # No values in array -------------------------------------------------------------------------------- /Chapter06/1.2.8.Switch_Expression.ps1: -------------------------------------------------------------------------------- 1 | switch (Get-Date) { 2 | { $_ -is [DateTime] } { Write-Host 'This is a DateTime type' } 3 | { $_.Year -ge 2017 } { Write-Host 'It is 2017 or later' } 4 | } -------------------------------------------------------------------------------- /Chapter09/1.4.4.RepetitionAndPeriod.ps1: -------------------------------------------------------------------------------- 1 | 'Anything' -match '.*' # 0 or more. Returns true 2 | '' -match '.*' # 0 or more. Returns true 3 | 'Anything' -match '.+' # 1 or more. Returns true -------------------------------------------------------------------------------- /Chapter14/2.3.9_CreateAndDisconnect.ps1: -------------------------------------------------------------------------------- 1 | $session = New-PSSession PSTest -Name 'Example' 2 | Invoke-Command { Start-Sleep -Seconds (60 * 60) } -Session $session -AsJob 3 | Disconnect-PSSession $session -------------------------------------------------------------------------------- /Chapter16/1.4.2_CBH_script.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Briefly describes the main action performed by script.ps1 4 | .DESCRIPTION 5 | A detailed description of the activities of script.ps1. 6 | #> -------------------------------------------------------------------------------- /Chapter17/2.1.5.PSTypeNameAttribute_Validation.ps1: -------------------------------------------------------------------------------- 1 | function Test-PSTypeName { 2 | [CmdletBinding()] 3 | param ( 4 | [PSTypeName('SomeTypeName')] 5 | $InputObject 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/2.2.3.ValidateNotNull_AllowsEmpty.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateNotNull { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateNotNull()] 5 | [String[]]$Parameter1 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter17/6.1.1.dynamicparam.ps1: -------------------------------------------------------------------------------- 1 | function Test-DynamicParam { 2 | [CmdletBinding()] 3 | param ( ) 4 | 5 | dynamicparam { } 6 | end { 7 | Write-Host 'Function body' 8 | } 9 | } -------------------------------------------------------------------------------- /Chapter17/6.2.2.RuntimeDefinedParameter_paramEquivalent.ps1: -------------------------------------------------------------------------------- 1 | param ( 2 | [Parameter(Mandatory, Position = 1)] 3 | [ValidateSet('Start', 'Stop', 'Create', 'Delete')] 4 | [String]$Action 5 | ) -------------------------------------------------------------------------------- /Chapter18/1.2.3.enum_NewToPS62.ps1: -------------------------------------------------------------------------------- 1 | # Will only execute in development builds of PowerShell 6.2 at this time. 2 | 3 | enum MyEnum : UInt64 { 4 | First = 0 5 | Last = 18446744073709551615 6 | } -------------------------------------------------------------------------------- /Chapter18/1.5.4.enum_composite_flags.ps1: -------------------------------------------------------------------------------- 1 | [Flags()] 2 | enum MyEnum { 3 | First = 1 # 001 4 | Second = 2 # 010 5 | Third = 4 # 100 6 | FirstAndThird = 5 # 101 7 | } -------------------------------------------------------------------------------- /Chapter20/2.11.2_Mocks.ps1: -------------------------------------------------------------------------------- 1 | Describe Set-ComputerDescription { 2 | BeforeAll { 3 | Mock Set-ItemProperty 4 | Mock Clear-ItemProperty 5 | Mock Remove-ItemProperty 6 | } 7 | } -------------------------------------------------------------------------------- /Chapter21/4.2.7_Catch_InnerException_ArgumentOutOfRangeException.ps1: -------------------------------------------------------------------------------- 1 | try { 2 | [DateTime]::DaysInMonth(2019, 13) 3 | } catch [ArgumentOutOfRangeException] { 4 | Write-Host 'Out of range' 5 | } -------------------------------------------------------------------------------- /Chapter03/5.1.2.Group-Object_NoElement.ps1: -------------------------------------------------------------------------------- 1 | 6, 7, 7, 8, 8, 8 | Group-Object -NoElement 2 | 3 | # Expects output: 4 | # 5 | # Count Name 6 | # ----- ---- 7 | # 1 6 8 | # 2 7 9 | # 3 8 -------------------------------------------------------------------------------- /Chapter04/3.2.1.ComparisonAndArrays.ps1: -------------------------------------------------------------------------------- 1 | 1, $null -ne $null # Returns 1 2 | 1, 2, 3, 4 -ge 3 # Returns 3, 4 3 | 'one', 'two', 'three' -like '*e*' # Returns one and three -------------------------------------------------------------------------------- /Chapter05/7.6.13.RemovingElements_ByValue_IndexOf_New.ps1: -------------------------------------------------------------------------------- 1 | $index = $oldArray.IndexOf(50) 2 | if ($index -gt -1) { 3 | $newArray = $oldArray[0..($index - 1)] + 4 | $oldArray[($index + 1)..99] 5 | } -------------------------------------------------------------------------------- /Chapter05/9.2.1.Dictionary.ps1: -------------------------------------------------------------------------------- 1 | $dictionary = New-Object System.Collections.Generic.Dictionary"[String,IPAddress]" 2 | $dictionary = New-Object "System.Collections.Generic.Dictionary[String,IPAddress]" 3 | -------------------------------------------------------------------------------- /Chapter11/2.1.7_Interpolation_and_sub-expressions.ps1: -------------------------------------------------------------------------------- 1 | Get-CimInstance Win32_Process -Filter "ProcessId=$PID" 2 | Get-CimInstance Win32_Process -Filter "ExecutablePath LIKE '$($pshome -replace '\\', '\\')%'" -------------------------------------------------------------------------------- /Chapter15/1.3.1.Wait-Job_Alternative.ps1: -------------------------------------------------------------------------------- 1 | while (Get-Job -State Running) { 2 | $jobs = Get-Job -State Completed 3 | $jobs | Receive-Job 4 | $jobs | Remove-Job 5 | Start-Sleep -Seconds 1 6 | } -------------------------------------------------------------------------------- /Chapter17/2.3.1.AllowNull.ps1: -------------------------------------------------------------------------------- 1 | function Test-AllowNull { 2 | [CmdletBinding()] 3 | param ( 4 | [Parameter(Mandatory)] 5 | [AllowNull()] 6 | [Object]$Parameter1 7 | ) 8 | } -------------------------------------------------------------------------------- /Chapter18/5.5.3.Dsc_ComputerDescription_using_module.ps1: -------------------------------------------------------------------------------- 1 | using module LocalMachine 2 | 3 | $class = [ComputerDescription]@{ 4 | Ensure = 'Present' 5 | Description = 'Computer description' 6 | } -------------------------------------------------------------------------------- /Chapter20/2.3.3_IndependentVerification_Error.ps1: -------------------------------------------------------------------------------- 1 | Get-SquareRoot 200 | Should -Be ([Math]::Sqrt(200)) 2 | 3 | # Expected output (error): 4 | # Expected 14.142135623731, but got 14.142135623730950488016887242. -------------------------------------------------------------------------------- /Chapter20/2.6.2_BeforeAll.ps1: -------------------------------------------------------------------------------- 1 | BeforeAll { 2 | $extensions = '.txt', '.log', '.doc' 3 | $Path = 'C:\Temp\StaleFiles' 4 | $null = New-Item $Path -ItemType Directory 5 | Push-Location $Path 6 | } -------------------------------------------------------------------------------- /Chapter05/7.1.2.CreateArrayOfSize.ps1: -------------------------------------------------------------------------------- 1 | $myArray = New-Object Object[] 10 # 10 objects 2 | $byteArray = New-Object Byte[] 100 # 100 bytes 3 | $ipAddresses = New-Object IPAddress[] 5 # 5 IP addresses -------------------------------------------------------------------------------- /Chapter05/8.2.9.KeysArray.ps1: -------------------------------------------------------------------------------- 1 | $hashtable = @{ 2 | Key1 = 'Value1' 3 | Key2 = 'Value2' 4 | } 5 | [Object[]]$keys = $hashtable.Keys 6 | foreach ($key in $keys) { 7 | $hashtable[$key] = "NewValue" 8 | } -------------------------------------------------------------------------------- /Chapter07/5.3.1.CreationFromHashtable_Constructor.ps1: -------------------------------------------------------------------------------- 1 | $searcher = [ADSISearcher]::new( 2 | [ADSI]'LDAP://domain.com', 3 | '(&(objectClass=user)(objectCategory=person))' 4 | ) 5 | $searcher.PageSize = 1000 -------------------------------------------------------------------------------- /Chapter08/1.1.1.IndexIntoString.ps1: -------------------------------------------------------------------------------- 1 | $myString = 'abcdefghijklmnopqrstuvwxyz' 2 | $myString[0] # This is a (the first character in the string) 3 | $myString[-1] # This is z (the last character in the string) -------------------------------------------------------------------------------- /Chapter08/3.2.4.Math_RoundMethodMidpointRounding.ps1: -------------------------------------------------------------------------------- 1 | [Math]::Round(2.225, 2) # Results in 2.22 2 | [Math]::Round(2.225, 2, [MidpointRounding]::AwayFromZero) # Results in 2.23 -------------------------------------------------------------------------------- /Chapter12/3.1.2_PropertyExpansion.ps1: -------------------------------------------------------------------------------- 1 | $xml.cars.car 2 | 3 | # Expects: 4 | # 5 | # type : Saloon 6 | # colour : Green 7 | # doors : 4 8 | # transmission : Automatic 9 | # engine : engine -------------------------------------------------------------------------------- /Chapter13/1.5.1_WebClient_ServicePointManager.ps1: -------------------------------------------------------------------------------- 1 | [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } 2 | [System.Net.WebClient]::new().DownloadString('https://expired.badssl.com/') -------------------------------------------------------------------------------- /Chapter13/3.2.5_Enum.ps1: -------------------------------------------------------------------------------- 1 | [SOAP.Group] 2 | 3 | # Expects: 4 | # 5 | # IsPublic IsSerial Name BaseType 6 | # -------- -------- ---- -------- 7 | # True True Group System.Enum -------------------------------------------------------------------------------- /Chapter16/3.3.4_Default_value_throw.ps1: -------------------------------------------------------------------------------- 1 | # Will not run in the console as-is. Needs to be in a .ps1 file, or wrapped by function. 2 | param ( 3 | [String]$Parameter1 = (throw 'This parameter is mandatory') 4 | ) -------------------------------------------------------------------------------- /Chapter17/1.2.3.PositionalBinding_EffectivelyDisabled_Function.ps1: -------------------------------------------------------------------------------- 1 | function Test-Position { 2 | param ( 3 | [Parameter(Position = 1)] 4 | $Parameter1, 5 | 6 | $Parameter2 7 | ) 8 | } -------------------------------------------------------------------------------- /Chapter17/2.2.16.ValidateScript.ps1: -------------------------------------------------------------------------------- 1 | function Test-ValidateScript { 2 | [CmdletBinding()] 3 | param ( 4 | [ValidateScript( { Test-Path $_ -PathType Leaf } )] 5 | [String]$Path 6 | ) 7 | } -------------------------------------------------------------------------------- /Chapter03/6.1.4.Compare-Object_ExcludeDifferent_PassThru.ps1: -------------------------------------------------------------------------------- 1 | Compare-Object -ReferenceObject 1, 2, 3, 4 -DifferenceObject 1, 2 -ExcludeDifferent -IncludeEqual -PassThru 2 | 3 | # Expects output: 4 | # 5 | # 1 6 | # 2 -------------------------------------------------------------------------------- /Chapter03/7.1.7.ConvertTo-Csv_Arrays_Why.ps1: -------------------------------------------------------------------------------- 1 | $object = [PSCustomObject]@{ 2 | Name = "Numbers" 3 | Value = 1, 2, 3, 4, 5 4 | } 5 | $object.Value.ToString() 6 | 7 | # Expects output: 8 | # 9 | # System.Object[] -------------------------------------------------------------------------------- /Chapter04/9.4.4.PreIncrementExample.ps1: -------------------------------------------------------------------------------- 1 | $array = 1..5 2 | $i = 0 3 | do { 4 | # $i is incremented before use, 2 will be the first printed. 5 | Write-Host $array[++$i] 6 | } while ($i -lt $array.Count - 1) 7 | -------------------------------------------------------------------------------- /Chapter05/2.4.2.Remove-Variable_ReferenceOnly.ps1: -------------------------------------------------------------------------------- 1 | $object1 = $object2 = Get-Process | Select-Object -First 1 2 | Remove-Variable object1 3 | Write-Host $object2.Name 4 | 5 | # Expects similar to: 6 | # 7 | # conhost -------------------------------------------------------------------------------- /Chapter05/9.2.8.AccessByKey_StronglyTypedKey.ps1: -------------------------------------------------------------------------------- 1 | $dictionary = New-Object System.Collections.Generic.Dictionary"[String,IPAddress]" 2 | $dictionary.Add("1", "192.168.10.222") 3 | $dictionary.Add("2", "192.168.10.13") -------------------------------------------------------------------------------- /Chapter07/5.3.2.CreationFromHashtable.ps1: -------------------------------------------------------------------------------- 1 | $searcher = [ADSISearcher]@{ 2 | SearchRoot = [ADSI]'LDAP://domain.com' 3 | Filter = '(&(objectClass=user)(objectCategory=person))' 4 | PageSize = 1000 5 | } -------------------------------------------------------------------------------- /Chapter08/2.1.4.ToBase64String_Unicode.ps1: -------------------------------------------------------------------------------- 1 | $bytes = [System.Text.Encoding]::Unicode.GetBytes('Hello world') 2 | [Convert]::ToBase64String($bytes) 3 | 4 | # Expects output: 5 | # 6 | # SABlAGwAbABvACAAdwBvAHIAbABkAA== -------------------------------------------------------------------------------- /Chapter09/3.1.1.GreedyQuanifier.ps1: -------------------------------------------------------------------------------- 1 | 'C:\long\path\to\some\files' -match '.*\\' 2 | 3 | # Expects output: 4 | # 5 | # True 6 | 7 | $matches[0] 8 | 9 | # Expects output: 10 | # 11 | # C:\long\path\to\some\ -------------------------------------------------------------------------------- /Chapter09/4.1.4.Range_HexadecimalNumber.ps1: -------------------------------------------------------------------------------- 1 | 'The registry value is 0xAF9B7' -match '0x[0-9a-f]+' 2 | 3 | # Expects output: 4 | # 5 | # True 6 | 7 | $matches[0] 8 | 9 | # Expects output: 10 | # 11 | # 0xAF9B7 -------------------------------------------------------------------------------- /Chapter09/5.1.2.Alternation_Example.ps1: -------------------------------------------------------------------------------- 1 | 'one', 'one hundred', 'three', 'eighty three' | Where-Object { $_ match '^one|three$' } 2 | 3 | # Expects output: 4 | # 5 | # one 6 | # one hundred 7 | # three 8 | # eighty three -------------------------------------------------------------------------------- /Chapter11/3.3.4_Invoke-CimMethod_output.ps1: -------------------------------------------------------------------------------- 1 | $return 2 | 3 | # Expects similar to: 4 | # 5 | # ProcessId ReturnValue PSComputerName 6 | # --------- ----------- -------------- 7 | # 15172 0 --------------------------------------------------------------------------------