\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{giangpdh} \pdfinfo{ /Title (powershell.pdf) /Creator (Cheatography) /Author (giangpdh) /Subject (PowerShell Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{0593A3} \definecolor{LightBackground}{HTML}{EFF8F9} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{PowerShell Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{giangpdh} via \textcolor{DarkBackground}{\uline{cheatography.com/137135/cs/36171/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}giangpdh \\ \uline{cheatography.com/giangpdh} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 19th December, 2022.\\ Updated 21st December, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Working with variables}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\}To create a new variable, use an assignment statement to assign a value to the variable. & \$MyVariable = 1, 2, 3\{\{nl\}\}\$Path = "C:\textbackslash{}Windows\textbackslash{}System32" & \tn % Row Count 8 (+ 8) % Row 1 \SetRowColor{white} To display the value of a variable, type the variable name, preceded by a dollar sign (\$). & \$MyVariable & 1\{\{nl\}\}2\{\{nl\}\}3 \tn % Row Count 15 (+ 7) % Row 2 \SetRowColor{LightBackground} To change the value of a variable, assign a new value to the variable. & \$MyVariable = "The green cat."\{\{nl\}\}\$MyVariable & The green cat. \tn % Row Count 21 (+ 6) % Row 3 \SetRowColor{white} To delete the value of a variable, use the \seqsplit{Clear-Variable} cmdlet or change the value to \$null. & \seqsplit{Clear-Variable} -Name MyVariable \{\{nl\}\}\$MyVariable = \$null & \tn % Row Count 29 (+ 8) % Row 4 \SetRowColor{LightBackground} To delete the variable, use \seqsplit{Remove-Variable} or Remove-Item. & \seqsplit{Remove-Variable} -Name MyVariable\{\{nl\}\}Remove-Item -Path Variable:\textbackslash{}MyVariable & \tn % Row Count 35 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.584 cm} x{2.508 cm} x{2.508 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Working with variables (cont)}} \tn % Row 5 \SetRowColor{LightBackground} \mymulticolumn{3}{x{8.4cm}}{To get a list of all the variables in your PowerShell session, type Get-Variable.} \tn % Row Count 2 (+ 2) % Row 6 \SetRowColor{white} Variables are useful for storing the results of commands. & \$Processes = Get-Process\{\{nl\}\}\$Today = \seqsplit{(Get-Date).DateTime} & \tn % Row Count 7 (+ 5) % Row 7 \SetRowColor{LightBackground} It is also possible to assign values to multiple variables with one statement. & \$a = \$b = \$c = 0 & \tn % Row Count 13 (+ 6) % Row 8 \SetRowColor{white} The next example assigns multiple values to multiple variables. & \$i,\$j,\$k = 10, "red", \$true\{\{nl\}\}\$i,\$j = 10, "red", \$true & \# \$i is 10, \$j is "red", \$k is True\{\{nl\}\}\# \$i is 10, \$j is {[}object{[}{]}{]}, Length 2 \tn % Row Count 20 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4.24 cm} x{3.76 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Types of variables}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=50\}\}\$a = 12 & System.Int32 \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \$a = "Word" & System.String \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \$a = 12, "Word" & array of System.Int32, System.String \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \$a = Get-ChildItem C:\textbackslash{}Windows & FileInfo and DirectoryInfo types \tn % Row Count 6 (+ 2) % Row 4 \SetRowColor{LightBackground} To use cast notation, enter a type name, enclosed in brackets, before the variable name (on the left side of the assignment statement). & {[}int{]}\$number = 8 \tn % Row Count 13 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.04 cm} x{4.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Variable substitution in strings}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\}Concatenation & \$name = 'Kevin Marquette' \{\{nl\}\}\$message = 'Hello, ' + \$name \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Variable substitution & \$first = 'Kevin' \{\{nl\}\}\$last = 'Marquette' \{\{nl\}\}\$message = "Hello, \$first \$last." \tn % Row Count 7 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Arrays}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=40\}\}To create and initialize an array, assign multiple values to a variable. & \$A = 22,5,10,8,12,9,80 \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} The array sub-expression operator creates an array from the statements inside it. & @( ... ) \{\{nl\}\}\$a = @("Hello World") \{\{nl\}\}\$p = @(Get-Process Notepad) \tn % Row Count 10 (+ 5) % Row 2 \SetRowColor{LightBackground} Where-Object filtering & \$data | Where-Object \{\$\_.FirstName -eq 'Kevin'\} \{\{nl\}\} \$data | Where FirstName -eq Kevin \tn % Row Count 15 (+ 5) % Row 3 \SetRowColor{white} Where() & \$data.Where(\{\$\_.FirstName -eq 'Kevin'\}) \tn % Row Count 17 (+ 2) % Row 4 \SetRowColor{LightBackground} Selects objects or object properties. & Get-Process | Select-Object -Property ProcessName, Id, WS \tn % Row Count 20 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Hash Tables}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=50\}\}To create an empty hashtable in the value of \$hash, type: & \$hash = @\{\} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} You can also add keys and values to a hashtable when you create it. & \$hash = @\{ Number = 1; Shape = "Square"; Color = "Blue"\} \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} To display a hashtable that's saved in a variable, type the variable name. & \$hash \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} hashtables have Keys and Values properties. & \$hash.keys\{\{nl\}\}\$hash.values \tn % Row Count 15 (+ 3) % Row 4 \SetRowColor{LightBackground} You can iterate over the keys in a hashtable to process the values in several ways. & foreach (\$Key in \$hash.Keys) \{\{\{nl\}\}~~~~"The value of '\$Key' is: \$(\$hash{[}\$Key{]})" \{\{nl\}\}\} \tn % Row Count 21 (+ 6) % Row 5 \SetRowColor{white} To add keys and values to a hashtable, use the following command format. & \$hash{[}"\textless{}key\textgreater{}"{]} = "\textless{}value\textgreater{}" \{\{nl\}\}\$hash{[}"Time"{]} = "Now" \tn % Row Count 25 (+ 4) % Row 6 \SetRowColor{LightBackground} You can also add keys and values to a hashtable using the Add method of the \seqsplit{System.Collections.Hashtable} object. & Add(Key, Value) \{\{nl\}\}\$hash.Add("Time", "Now") \tn % Row Count 31 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{PSCustomObject}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=30\}\}Creating a PSCustomObject & \$myObject = {[}PSCustomObject{]}@\{\{\{nl\}\} ~~~~Name = 'Kevin' \{\{nl\}\}~~~~Language = 'PowerShell' \{\{nl\}\}~~~~State = 'Texas' \{\{nl\}\}\} \tn % Row Count 11 (+ 11) % Row 1 \SetRowColor{white} Converting a hashtable & \$myHashtable = @\{\{\{nl\}\} ~~~~Name = 'Kevin' \{\{nl\}\}~~~~Language = 'PowerShell' \{\{nl\}\}~~~~State = 'Texas' \{\{nl\}\}\} \{\{nl\}\}\{\{nl\}\}\$myObject = {[}pscustomobject{]}\$myHashtable \tn % Row Count 24 (+ 13) % Row 2 \SetRowColor{LightBackground} Saving to a file & \$myObject | ConvertTo-Json -depth 1 | Set-Content -Path \$Path \{\{nl\}\}\$myObject = Get-Content -Path \$Path | ConvertFrom-Json \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{3.92 cm} x{4.08 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{PSCustomObject (cont)}} \tn % Row 3 \SetRowColor{LightBackground} Adding properties & \$myObject | Add-Member -MemberType NoteProperty -Name 'ID' -Value 'KevinMarquette'\{\{nl\}\}\$myObject.ID \tn % Row Count 5 (+ 5) % Row 4 \SetRowColor{white} Remove properties & \seqsplit{\$myObject.psobject.properties.remove('ID')} \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{LightBackground} Enumerating property names & \$myObject | Get-Member -MemberType NoteProperty | Select -ExpandProperty Name \tn % Row Count 12 (+ 4) % Row 6 \SetRowColor{white} Dynamically accessing properties & \$myObject.'Name' \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{LightBackground} Convert PSCustomObject into a hashtable & \$hashtable = @\{\} \{\{nl\}\}foreach( \$property in \seqsplit{\$myobject.psobject.properties.name} ) \{\{nl\}\}\{ \{\{nl\}\}~~~~\$hashtable{[}\$property{]} = \seqsplit{\$myObject.\$property} \{\{nl\}\}\} \tn % Row Count 23 (+ 9) % Row 8 \SetRowColor{white} Testing for properties & if( \$null -ne \$myObject.ID )\{\{nl\}\}if( \seqsplit{\$myobject.psobject.properties.match('ID').Count} ) \tn % Row Count 28 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{3.36 cm} x{4.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Functions}} \tn % Row 0 \SetRowColor{LightBackground} \{\{width=30\}\}A simple function & function Get-Version \{ \{\{nl\}\} ~~~~ \seqsplit{\$PSVersionTable.PSVersion} \{\{nl\}\} \} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Parameters & function Test-MrParameter \{ \{\{nl\}\}~~~~ param ( \{\{nl\}\} ~~~~~~~~ \$ComputerName \{\{nl\}\} ~~~~ ) \{\{nl\}\} ~~~~ Write-Output \$ComputerName \{\{nl\}\} \} \tn % Row Count 15 (+ 11) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}