diff options
author | claw0ry <me@claw0ry.net> | 2024-12-11 13:56:52 +0100 |
---|---|---|
committer | claw0ry <me@claw0ry.net> | 2024-12-11 13:56:52 +0100 |
commit | 4719cc03837490ed4bf1b9725d75a686e56e5a6a (patch) | |
tree | 769dd3a3a87153df049b3043196bd131495b10ad /content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md |
fresh start
Diffstat (limited to 'content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md')
-rw-r--r-- | content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md b/content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md new file mode 100644 index 0000000..932accc --- /dev/null +++ b/content/posts/basic-tasks-to-get-you-started-automating-with-powershell.md @@ -0,0 +1,104 @@ +--- +title: "Basic tasks to get you started automating with Powershell" +date: 2024-04-05T22:00:00+02:00 +tags: ['Powershell'] +draft: true +--- + +Here are 10 common tasks that you will need to master when automating in Powershell (or any language for that matter). + +## 1. Reading and writing files + +```powershell +# Let's write something to a file +PS> "Hello, World!" | Out-File -FilePath myfile.txt + +# Read contents of file +PS> Get-Content -Path myfile.txt +Hello, World! + +# Shorthand +PS> gc myfile.txt +Hello, World! +``` + +## 2. Working with data + +### Convert Powershell object to JSON + +```powershell +PS> $myobj = [PSCustomObject]@{ +> Name = "dotpwsh" +> Homepage = "https://dotpwsh.com" +> Twitter = "@moiaune" +> } +PS> $myobj | ConvertTo-Json +{ + "Name": "dotpwsh", + "Homepage": "https://dotpwsh.com", + "Twitter": "@moiaune" +} +``` + +### Convert JSON string to Powershell object + +```powershell +PS> $jsonData = @" +> { +> "Name": "dotpwsh", +> "Homepage": "https://dotpwsh.com", +> "Youtube": "https://youtube.com/@moiaune" +> } +> "@ +PS> $jsonData +{ + "Name": "dotpwsh", + "Homepage": "https://dotpwsh.com", + "Youtube": "https://youtube.com/@moiaune" +} +PS> $jsonData | ConvertFrom-Json + +Name Homepage Youtube +---- -------- ------- +dotpwsh https://dotpwsh.com https://youtube.com/@moiaune + +``` + +### Convert CSV string to Powershell object + +```powershell +PS> $csvData = @" +> name,homepage,twitter +> dotpwsh,https://dotpwsh.com,@moiaune +> "@ +PS> $csvData +name,homepage,twitter +dotpwsh,https://dotpwsh.com,@moiaune +PS> $csvData | ConvertFrom-Csv + +name homepage twitter +---- -------- ------- +dotpwsh https://dotpwsh.com @moiaune +``` + +### Convert Powershell object to CSV + +```powershell +PS> $myobj = [PSCustomObject]@{ +> Name = "dotpwsh" +> Homepage = "https://dotpwsh.com" +> Twitter = "@moiaune" +> } +PS> $myobj | ConvertTo-Csv +"Name","Homepage","Twitter" +"dotpwsh","https://dotpwsh.com","@moiaune" +``` + + +## 3. Interacting with REST API's + + +## 4. Archiving and extracting files + + +## 5. Working with Regular Expresions |