From 458c3f70199e3b3f81054b9f1d2c7b862b738a69 Mon Sep 17 00:00:00 2001 From: RealStickman Date: Fri, 13 Nov 2020 14:56:14 +0100 Subject: [PATCH] Add lots of new info - Arrays - Files and directories --- .../programming/powershell-scripting.md | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/arch-config/Dokumente/programming/powershell-scripting.md b/arch-config/Dokumente/programming/powershell-scripting.md index 057ef3ba..598917f2 100644 --- a/arch-config/Dokumente/programming/powershell-scripting.md +++ b/arch-config/Dokumente/programming/powershell-scripting.md @@ -13,6 +13,84 @@ Use PowerShell ISE to develop PowerShell scripts | -lt | less than | | -le | less or equals to | +## Arrays +Initialise arrays like this: +``` +$(arrayname) = @("item1", "item2", "item3") +``` + +There are two methods for looping over arrays: + +**Method 1** +``` +foreach ($(itemname) in $(arrayname)) { + (command) + //To get the item from the array use $(itemname) +} +``` + +**Method 2** +``` +for ($i = 0; $i -lt $(arrayname).count; $i++) { + (command) + //To get the item from the array use $(arrayname)[$i] +} +``` + +## Operations in the filesystem +Test whether a file or directory exists +``` +Test-Path "(path)" +``` + +### files +Create a new file +``` +New-Item -Path "(filepath)" -ItemType File +``` + +Remove a file +``` +Remove-Item "(filepath)" +``` + +Set file content +``` +Set-Content "(filepath)" ("(content)") +``` + +Example using multiple lines +``` +Set-Content "C:\temp\test.txt" ("This is a very complex sentence. " + "`r`n" + "This sentence should be on the second line.") +``` + +Apend content to a file +``` +Add-Content "(filepath)" ("(content)") +``` + +Show a file's content +``` +Get-Content "(filepath)" +``` + +### directories +Create new directory +``` +New-Item -Path "(directorypath)" -ItemType Directory +``` + +Remove directory including contained files +``` +Remove-Item "(directorypath)" -Recurse +``` + +Copy directory +``` +Copy-Item "(inputpath)" -Recurse "(destinationpath)" +``` + + ## Remoting ### Allow recieving remote commands