\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{\_HolyHustler\_} \pdfinfo{ /Title (basic-powershell-cheat-sheet.pdf) /Creator (Cheatography) /Author (\_HolyHustler\_) /Subject (Basic 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}{2571A3} \definecolor{LightBackground}{HTML}{F1F6F9} \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{Basic Powershell Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{\_HolyHustler\_} via \textcolor{DarkBackground}{\uline{cheatography.com/139195/cs/37459/}}} \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}\_HolyHustler\_ \\ \uline{cheatography.com/holyhustler} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 2nd March, 2023.\\ 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*}{3} \begin{tabularx}{5.377cm}{p{0.64701 cm} x{4.32999 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Définitions}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{Cmlets} & Commandes intégrées dans un shell écrit en .NET. \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Comparaison deux fichiers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Chemin des fichiers à comparer \newline \$file1 = "C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}List1.txt" \newline \$file2 = "C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}List2.txt" \newline \newline \# Date du rapport \newline \$date = Get-Date -Format "dd/MM/yyyy" \newline \newline \# Charger les listes d'ordinateurs dans des variables \newline \$list1 = Get-Content \$file1 \newline \$list2 = Get-Content \$file2 \newline \newline \# Comparer les listes d'ordinateurs \newline \$diff1 = \$list2 | Where-Object \{\$\_ -notin \$list1\} \newline \$diff2 = \$list1 | Where-Object \{\$\_ -notin \$list2\} \newline \newline \# Générer le rapport \newline Write-Host "Rapport de comparaison le \$date" \newline \newline if (\$diff1) \{ \newline Write-Host "Ces ordinateurs ne sont pas dans \$file1" \newline Write-Host (\$diff1 -join "`n") \newline Write-Host " " \newline \} \newline \newline if (\$diff2) \{ \newline Write-Host "Ces ordinateurs ne sont pas dans \$file2"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Synchroniser deux répertoires}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\# Spécifiez les chemins des répertoires source et destination \newline \$chemin\_source = "C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}Dossier1" \newline \$chemin\_destination = "C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}Dossier2" \newline \newline \# Parcourez tous les fichiers du répertoire source \newline Get-ChildItem \$chemin\_source -Recurse | ForEach-Object \{ \newline \# Vérifiez si le fichier existe déjà dans le répertoire de destination \newline \$fichier\_destination = \seqsplit{\$\_.FullName.Replace(\$chemin\_source}, \$chemin\_destination) \newline \# Remplace le chemin du fichier C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}Dossier1\textbackslash{}example.txt par C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}Dossier2\textbackslash{}example.txt \newline if (!(Test-Path \$fichier\_destination)) \{ \newline \# Si le fichier n'existe pas dans le répertoire de destination, copiez-le \newline Copy-Item \$\_.FullName \$fichier\_destination \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.63781 cm} x{2.33919 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Sessions distantes}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Enter-PSSession}} Server01 & Ouvrir une session interactive. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Exit-PSSession}} & Fermer une session interactive. \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Invoke-Command}} -ComputerName Server01, Server02 -ScriptBlock \{Get-UICulture\} & Invoquer une commande à distance. \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} {\bf{Invoke-Command}} -ComputerName Server01, Server02 -FilePath C:\textbackslash{}Scripts\textbackslash{}script.ps1 & Exécuter une commande à distance. \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} \$s = New-PSSession -ComputerName Server01, Server02\{\{nl\}\}{\bf{Invoke-Command}} -Session \$s \{\$h = Get-HotFix\}\{\{nl\}\}{\bf{Import-PSSession}} -Session \$session & Etablir une connexion persistante. \tn % Row Count 20 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Commandes Basiques}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{pwsh}} & Lancer powershell sur Linux. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Set-ExecutionPolicy}} -ExecutionPolicy Unrestricted & A exécuter en administrateur, permet l'exécution de scripts. \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} {\bf{Get-Help}} {\emph{commande}} & Equivalent de man, permet d'affiche l'aide concernant une commande. \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} {\bf{Get-Command}} -Name *ip* & Permet de cherche une commande. \tn % Row Count 12 (+ 2) % Row 4 \SetRowColor{LightBackground} {\bf{Get-Uptime}} | {\bf{Get-Member}} & Permet de lister toutes les méthodes disponibles pour une commande. \tn % Row Count 16 (+ 4) % Row 5 \SetRowColor{white} {\bf{Select-Object}} {\emph{objet1,objet2}} & Permet de filtrer des données d'un objet. \tn % Row Count 19 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Get-ChildItem}} -Path {\emph{"chemin"}} & Permet de lister les fichiers et répertoires d'un dossier. \tn % Row Count 22 (+ 3) % Row 7 \SetRowColor{white} {\bf{Invoke-WebRequest}} -Uri {\emph{"url"}} -OutFile {\emph{"chemin de sortie"}} & Permet de télécharger un fichier à partir d'internet. \tn % Row Count 26 (+ 4) % Row 8 \SetRowColor{LightBackground} {\bf{Resolve-DnsName}} {\emph{"nom de domaine"}} & Equivalent au nslookup. \tn % Row Count 28 (+ 2) % Row 9 \SetRowColor{white} {\bf{Test-Connection}} {\emph{ip}} -ComputerName {\emph{nom ordinateur}} -Count 3 & Equivalent au ping, ip ou nom d'ordinateur à utiliser. \tn % Row Count 32 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Commandes Basiques (cont)}} \tn % Row 10 \SetRowColor{LightBackground} {\bf{Get-HotFix}} & Récupère la liste des mises à jour. \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.9908 cm} x{2.9862 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Gestion Utilisateurs}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{New-LocalUser}} & Permet de créer un nouvel utilisateur local. \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{Get-LocalUser}} & Permet de lister les utilisateurs locaux existants sur le système. \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} {\bf{Set-LocalUser}} & Permet de modifier les paramètres d'un utilisateur local existant. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{Remove-LocalUser}} & Permet de supprimer un utilisateur local existant. \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{New-LocalGroup}} & Permet de créer un nouveau groupe local. \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} {\bf{Get-LocalGroup}} & Permet de lister les groupes locaux existants sur le système. \tn % Row Count 16 (+ 3) % Row 6 \SetRowColor{LightBackground} {\bf{Add-LocalGroupMember}} & Permet d'ajouter un utilisateur à un groupe local existant. \tn % Row Count 19 (+ 3) % Row 7 \SetRowColor{white} {\bf{Remove-LocalGroupMember}} & Permet de supprimer un utilisateur d'un groupe local existant. \tn % Row Count 22 (+ 3) % Row 8 \SetRowColor{LightBackground} {\bf{Set-LocalGroup}} & Permet de modifier les paramètres d'un groupe local existant. \tn % Row Count 25 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Remplacer du texte}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$search = 'zob' \newline \$replace = 'bwah' \newline \$path = 'C:*.txt' \newline \newline \newline (Get-Content -Path \$path ) -replace \$search,\$replace | Set-Content -path \$path} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Exemples}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{userpassword}} = \seqsplit{(ConvertTo-SecureString} "bwah" -AsPlainText -Force) \{\{nl\}\} {\bf{New-LocalUser}} "test" -Password \$userpassword \{\{nl\}\} {\bf{Add-LocalGroupMember}} -Group "Utilisateurs" -Member "test" & Créer un utilisateur test avec comme mot de passe bwah. Il faut toujours préciser le groupe. \tn % Row Count 10 (+ 10) % Row 1 \SetRowColor{white} {\bf{New-Item}} -Path "HKCU:\textbackslash{}Nom"\{\{nl\}\}{\bf{Set-ItemProperty}} -Path \$reg\_key\_path -Name Value1 -Value 1 -Type DWORD/String & Créer notre propre clé de registre. Choisir un des deux types, DWORD = hexadécimale, String = texte. \tn % Row Count 16 (+ 6) % Row 2 \SetRowColor{LightBackground} {\bf{Import-Module}} C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents\textbackslash{}mon-module.psm1\{\{nl\}\}{\bf{Remove-Module}} mon-module & Création de son module.\{\{nl\}\}On créer un fichier .psm \tn % Row Count 21 (+ 5) % Row 3 \SetRowColor{white} {\bf{New-Item}} -Path "C:\textbackslash{}Users\textbackslash{}admin\textbackslash{}Documents" -Name "List1.txt" -Value "PC1\textbackslash{}`nPC2\textbackslash{}`nPC5" & Création de fichier. \tn % Row Count 26 (+ 5) % Row 4 \SetRowColor{LightBackground} {\bf{Get-ChildItem}} -Path registery::\textbackslash{}HKEY\_CURRENT\_USER\textbackslash{}Console & Afficher des données de la base registre. \tn % Row Count 30 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Exemples (cont)}} \tn % Row 5 \SetRowColor{LightBackground} {\bf{Get-Process}} "{\emph{a}}" | Select-Object Name,CPU | Format-Table Name, CPU & Exemple de grosse commande. \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.89126 cm} x{3.08574 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Gestion Services \& Tâches}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Get-ScheduledTask}} & Permet de lister les tâches planifiées existantes sur le système. \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} {\bf{New-ScheduledTask}} & Permet de créer une nouvelle tâche planifiée. \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Set-ScheduledTask}} & Permet de modifier les paramètres d'une tâche planifiée existante. \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{Remove-ScheduledTask}} & Permet de supprimer une tâche planifiée existante. \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} {\bf{Get-Service}} & Permet de lister les services installés sur le système. \tn % Row Count 14 (+ 3) % Row 5 \SetRowColor{white} {\bf{Start-Service}} & Permet de démarrer un service. \tn % Row Count 16 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Stop-Service}} & Permet d'arrêter un service. \tn % Row Count 18 (+ 2) % Row 7 \SetRowColor{white} {\bf{Restart-Service}} & Permet de redémarrer un service. \tn % Row Count 20 (+ 2) % Row 8 \SetRowColor{LightBackground} {\bf{Set-Service}} & Permet de modifier les paramètres d'un service. \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Gestion carte réseau}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{\$ip = '192.168.1.4' \newline \$masque = 24 \newline \$passerelle = '192.168.1.2' \newline \$dns = '8.8.8.8' \newline \newline \newline Set-NetIPInterface -InterfaceAlias Ethernet -Dhcp Disabled \newline New-NetIPAddress -InterfaceAlias Ethernet -IPAddress \$ip -PrefixLength \$masque -DefaultGateway \$passerelle \newline Set-DnsClientServerAddress -interfaceAlias Ethernet -ServerAddresses \$dns} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}