Let’s say that we need to write a code responsible for replace a piece of information in an existing file, or maybe just clean a directory regularly.

To solve these scenarios, what tool do you think that can do the job in a simple way?

In my opinion, shell scrips are the right guys for this job. Why do I believe it? Because they are simple to write, easy to understand, and straightforward to maintain.

Right, so why do I need to use other tools to do the same job and get the same output? Actually you don’t, my idea is just to show another way :)


First things first

Here you can find the full documentation about Powershell installation.

Ok, no more talk, let’s code.

Replacing data into files using Powershell

Backing to our scenario, when we want to replace a piece of information in an existing file. And let’s say that we have a json with multiple levels like this:

	{
	  "NLog": {
	    "throwConfigExceptions": true,
	    "targets": {
	      "logfile": {
	        "type": "File",
	        "fileName": "/log/nlog-${shortdate}.log"
	      },
	      "logconsole": {
	        "type": "Console"
	      }
	    },
	    "rules": [
	      {
	        "logger": "*",
	        "minLevel": "Info",
	        "writeTo": "logconsole"
	      },
	      {
	        "logger": "*",
	        "minLevel": "Debug",
	        "writeTo": "logfile"
	      }
	    ]
	  }
	}

#shell #powershell #bash #json

When to use PowerShell instead of shell scripts?
1.20 GEEK