{"id":1568049,"date":"2023-03-07T00:03:26","date_gmt":"2023-03-06T18:33:26","guid":{"rendered":"https:\/\/gamingdeputy.com\/fr\/powershell-comment-ajouter-une-nouvelle-ligne-a-une-chaine-ou-a-une-variable\/"},"modified":"2023-03-07T00:03:26","modified_gmt":"2023-03-06T18:33:26","slug":"powershell-comment-ajouter-une-nouvelle-ligne-a-une-chaine-ou-a-une-variable","status":"publish","type":"post","link":"https:\/\/gamingdeputy.com\/fr\/powershell-comment-ajouter-une-nouvelle-ligne-a-une-chaine-ou-a-une-variable\/","title":{"rendered":"PowerShell &#8211; Comment ajouter une nouvelle ligne \u00e0 une cha\u00eene ou \u00e0 une variable\u00a0?"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\"><html><body><div itemprop=\"text\"><p>Le plus souvent, nous devons ajouter une nouvelle ligne entre les lignes pour la sortie de cha&icirc;ne afin de rendre la sortie nette et facile &agrave; lire. Utilisation de Windows <strong>Nouvelle gamme PowerShell<\/strong> peuvent &ecirc;tre facilement ajout&eacute;s &agrave; la sortie texte ou aux variables.<\/p><p>Dans cet article de blog, j&rsquo;expliquerai l&rsquo;ajout d&rsquo;une nouvelle ligne dans une sortie de cha&icirc;ne ou une variable, et comment utiliser le caract&egrave;re de nouvelle ligne ` (backtick) de PowerShell pour ajouter une nouvelle ligne.<\/p><p><strong>Astuce PowerShell&nbsp;:<\/strong> Si vous avez besoin d&rsquo;un retour chariot PowerShell, utilisez `r. Utilisez `n pour ajouter une nouvelle ligne PowerShell. Pour un retour chariot et une nouvelle ligne, utilisez `r`n<\/p><div><figure><figcaption>ajouter une nouvelle ligne PowerShell &agrave; la cha&icirc;ne (retour chariot)<\/figcaption><\/figure><\/div><p>Il existe diff&eacute;rentes fa&ccedil;ons d&rsquo;ajouter une nouvelle ligne &agrave; une cha&icirc;ne ou &agrave; une variable dans PowerShell en utilisant le retour chariot `r ou le caract&egrave;re de continuation de ligne ` &agrave; la fin du code.<\/p><p><strong>Recul (`)<\/strong> le personnage est le PowerShell <strong>caract&egrave;re de continuation de ligne<\/strong>. Il garantit que le script PowerShell continue jusqu&rsquo;&agrave; une nouvelle ligne.<\/p><p>Pour utiliser le caract&egrave;re de continuation de ligne dans PowerShell, tapez espace &agrave; la fin du code, utilisez le backtick ` et appuyez sur Entr&eacute;e pour continuer vers une nouvelle ligne.<\/p><h2><span>Ajouter une nouvelle ligne PowerShell &ndash; Utilisation du retour chariot `r<\/span><\/h2><p>Utilisez le caract&egrave;re sp&eacute;cial <strong>`r<\/strong> pour <strong>retour chariot<\/strong> dans PowerShell. L&rsquo;exemple ci-dessous montre comment utiliser <strong>`r Retour chariot PowerShell<\/strong><\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS C:&gt;\"Shell `r`nGeek\"<\/pre><p>Dans l&rsquo;exemple ci-dessus, il ajoute une nouvelle ligne avec PowerShell <strong>retour chariot<\/strong> (`r) et donne la sortie sous la forme<\/p><pre data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Shell\nGeek<\/pre><p><strong>Sais-tu:<\/strong> Comment utiliser la commande cat dans windows !<\/p><h2><span>PowerShell nouvelle ligne dans la sortie de cha&icirc;ne<\/span><\/h2><p>Utiliser <strong>`n<\/strong> pour ajouter une nouvelle ligne dans la sortie de cha&icirc;ne dans PowerShell. L&rsquo;exemple ci-dessous montre comment utiliser PowerShell nouvelle ligne `n dans la sortie de cha&icirc;ne.<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS C:&gt;\"Welcome to Shell Geek `nIts all about PowerShell\"<\/pre><p>Dans l&rsquo;exemple ci-dessus, en utilisant `n dans la cha&icirc;ne, ajoutez une nouvelle ligne dans la sortie de la cha&icirc;ne dans PowerShell. La sortie de la commande ci-dessus comme ci-dessous<\/p><pre data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Welcome to Shell Geek\nIts all about PowerShell<\/pre><p><strong>Astuce cool&nbsp;:<\/strong> Tail &ndash; Obtenez les derni&egrave;res lignes d&rsquo;un fichier dans PowerShell !<\/p><h2><span>Utilisation de la nouvelle ligne PowerShell dans la commande<\/span><\/h2><p>Le plus souvent, nous avons des commandes de script PowerShell plus longues et nous voulons les embellir en utilisant des instructions multilignes au lieu d&rsquo;une seule commande sur une seule ligne.<\/p><p>Pour ajouter une nouvelle ligne dans la commande de script PowerShell, utilisez la<strong>` (tic-tac)<\/strong> caract&egrave;re &agrave; la fin de chaque ligne pour diviser la commande en plusieurs lignes.<\/p><p>Dans l&rsquo;exemple ci-dessous, nous voulons obtenir de l&rsquo;espace disque libre sur un nom d&rsquo;ordinateur local est une commande &agrave; une seule ligne qui la rend tr&egrave;s difficile &agrave; lire et &agrave; g&eacute;rer.<\/p><p>Nous pouvons facilement le casser en utilisant <strong>`<\/strong> (caract&egrave;re de backtick PowerShell) pour la continuation de ligne dans une commande donn&eacute;e<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-CimInstance -ComputerName localhost win32_logicaldisk | where caption -eq \"C:\" | foreach-object {write \" $($_.caption) $('{0:N2}' -f ($_.Size\/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace\/1gb)) GB free \"}<\/pre><p>Apr&egrave;s avoir utilis&eacute; <strong>` <\/strong>(caract&egrave;re de backtick PowerShell) pour un saut de ligne dans la commande, il peut &ecirc;tre facilement lu<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Get-CimInstance -ComputerName localhost win32_logicaldisk `\n| where caption -eq \"C:\" `\n| foreach-object {write \" $($_.caption) $('{0:N2}' `\n-f ($_.Size\/1gb)) GB total, $('{0:N2}' `\n-f ($_.FreeSpace\/1gb)) GB free \"}<\/pre><p>Comme on le voit dans l&rsquo;exemple ci-dessus, utilisez le caract&egrave;re de continuation de ligne ` dans PowerShell pour continuer le code dans une nouvelle ligne.<\/p><p><strong>Astuce cool&nbsp;:<\/strong> Utilisez Tester la connexion pour envoyer un ping &agrave; une liste d&rsquo;ordinateurs dans PowerShell&nbsp;!<\/p><h2><span>Ajouter une nouvelle ligne PowerShell &agrave; la variable<\/span><\/h2><p>En utilisant <strong>Retour chariot PowerShell<\/strong> <code>`r or <\/code>`n, vous pouvez ajouter une nouvelle ligne PowerShell dans la variable ou une nouvelle ligne dans la sortie de cha&icirc;ne pour la variable comme ci-dessous<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS C:&gt;$strOutput = \"Welcome you to Shell Geek `r`nIts all about PowerShell\"\nPS C:&gt;$strOutput\nWelcome you to Shell Geek\nIts all about PowerShell<\/pre><p>Dans le script PowerShell ci-dessus pour ajouter une nouvelle ligne &agrave; une variable, nous d&eacute;finissons une variable avec un texte de cha&icirc;ne et ajoutons `n pour cr&eacute;er une nouvelle ligne pour la sortie de la variable.<\/p><p>Lorsque nous imprimons la variable strOutut sur le terminal, elle donne une sortie avec un saut de ligne entre la sortie de la cha&icirc;ne.<\/p><p><strong>Astuce cool&nbsp;:<\/strong> Remplacez le texte dans une cha&icirc;ne &agrave; l&rsquo;aide de PowerShell&nbsp;!<\/p><h2><span>Ajouter une nouvelle ligne PowerShell avec Write-Host <\/span><\/h2><p>Utilisation de `n pour ajouter une nouvelle ligne dans la sortie de cha&icirc;ne &agrave; l&rsquo;aide de PowerShell <code>Write-Host<\/code> commande. La commande ci-dessous imprime une nouvelle cha&icirc;ne de ligne sur la console<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PS C:&gt;Write-Host \"Welcome you to Shell Geek `nIts all about PowerShell\"\nWelcome you to Shell Geek\nIts all about PowerShell<\/pre><p>Dans le script ci-dessus, en utilisant PowerShell <code>Write-Host<\/code> newline est ajout&eacute; avec `n et imprime la cha&icirc;ne sur la console avec une nouvelle ligne dans le texte.<\/p><p><strong>Astuce cool&nbsp;:<\/strong> Savez-vous comment t&eacute;l&eacute;charger un fichier zip dans PowerShell&nbsp;?<\/p><h2><span>PowerShell Ajouter une nouvelle ligne au tableau<\/span><\/h2><p>Si vous souhaitez ajouter une nouvelle ligne au tableau &agrave; l&rsquo;aide de PowerShell, vous devez utiliser l&rsquo;op&eacute;rateur pipe pour diriger la variable de tableau vers Out-String.<\/p><p>Consid&eacute;rons un exemple de tableau Employee ayant des noms d&rsquo;employ&eacute;s stock&eacute;s dans une variable tableau comme ci-dessous<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$EmployeeArrayList=\"Tom Smith\",'Adam Strauss','Tim Smith','Gary willy'<\/pre><p>La variable $EmployeeArrayList contient des listes d&rsquo;employ&eacute;s s&eacute;par&eacute;s par une virgule. Pour imprimer le tableau ligne par ligne ou ajouter une nouvelle ligne au tableau &agrave; l&rsquo;aide du script PowerShell ci-dessous<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$EmployeeArrayList=\"Tom Smith\",'Adam Strauss','Tim Smith','Gary willy'\n$empArray = $EmployeeArrayList | Out-String\nWrite-Host $empArray<\/pre><p>Dans la premi&egrave;re commande, nous stockons la liste des employ&eacute;s dans la variable <strong>$EmployeeArrayList<\/strong><\/p><p>La deuxi&egrave;me commande utilise une variable tableau et dirige sa sortie vers <a target=\"_blank\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/out-string?view=powershell-7.2\" rel=\"noopener\">Out-String<\/a> et stocke le r&eacute;sultat dans un fichier temporaire <strong>$emTableau<\/strong> variable.<\/p><p>La troisi&egrave;me commande utilise l&rsquo;applet de commande PowerShell Write-host pour imprimer le tableau sur la console.<\/p><p>La sortie du script ci-dessus dans PowerShell pour ajouter une nouvelle ligne dans Array comme ci-dessous<\/p><div><figure><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/shellgeek.com\/wp-content\/uploads\/2021\/08\/powershell-new-line-to-array.png\" alt=\"Tableau d'impression PowerShell sur plusieurs lignes\" width=\"719\" height=\"210\" title=\"Tableau d'impression PowerShell sur plusieurs lignes\" srcset=\"https:\/\/shellgeek.com\/wp-content\/uploads\/2021\/08\/powershell-new-line-to-array.png 958w, https:\/\/shellgeek.com\/wp-content\/uploads\/2021\/08\/powershell-new-line-to-array-300x88.png 300w, https:\/\/shellgeek.com\/wp-content\/uploads\/2021\/08\/powershell-new-line-to-array-768x224.png 768w\" sizes=\"(max-width: 719px) 100vw, 719px\"><figcaption>Tableau d&rsquo;impression PowerShell sur plusieurs lignes<\/figcaption><\/figure><\/div><p><strong>Astuce cool&nbsp;:<\/strong> Concat&eacute;nation de cha&icirc;nes PowerShell avec des exemples&nbsp;!<\/p><h2><span>Conclusion<\/span><\/h2><p>J&rsquo;esp&egrave;re que vous avez trouv&eacute; utile l&rsquo;article ci-dessus sur les diff&eacute;rentes fa&ccedil;ons d&rsquo;ajouter une nouvelle ligne PowerShell &agrave; une sortie de variable ou de cha&icirc;ne dans PowerShell.<\/p><p>Utilisation du retour chariot PowerShell et de la nouvelle ligne PowerShell ` (caract&egrave;re de backtick) &agrave; la fin de chaque ligne pour diviser la commande en plusieurs lignes.<\/p><p>Vous pouvez &eacute;galement cr&eacute;er un nouveau saut de ligne PowerShell tout en d&eacute;finissant une cha&icirc;ne comme ci-dessous<\/p><pre data-enlighter-language=\"powershell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> PS C:&gt; \"Shell\n&gt;&gt;Geek\"\nShell\nGeek<\/pre><p>En savoir plus ici sur la cr&eacute;ation d&rsquo;une cha&icirc;ne multiligne dans PowerShell !<\/p><p>Vous pouvez trouver plus de rubriques sur les commandes PowerShell Active Directory et les bases de PowerShell sur la page d&rsquo;accueil de ShellGeek.<\/p><\/div><\/body><\/html>","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":1568050,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53116],"tags":[],"_links":{"self":[{"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/posts\/1568049"}],"collection":[{"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/comments?post=1568049"}],"version-history":[{"count":0,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/posts\/1568049\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/media\/1568050"}],"wp:attachment":[{"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/media?parent=1568049"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/categories?post=1568049"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gamingdeputy.com\/fr\/wp-json\/wp\/v2\/tags?post=1568049"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}