Forum Discussion
vivek10688
Jul 26, 2022Copper Contributor
Comment Or Un-Comment Multiple Lines In PowerShell Script
Problem Statement: I have a PowerShell script and before executing it I want to comment multiple lines using PowerShell command Series Of Steps Need To Performed 1. Comment multiple lines in Power...
vivek10688
Jul 27, 2022Copper Contributor
Hi Lain,
Thanks for writing the answer.
Objective :
Let say you have the content of powershell script in variable called $filecontent and then you do iteration in foreach loop where you do match string like 'RestartNumber -le 1' of if block and then gets line number and insert '<#' before the linenumber and eventually using same logic to get the line number of end of the if block and insert '#>' after the linenumber. This way the second if block will be commented out and will not be executed during execution of powershell script The purpose is simple comment the if block which you do not want to get it execute but not manually instead through powershell script.
Thanks
Vivek
Thanks for writing the answer.
Objective :
Let say you have the content of powershell script in variable called $filecontent and then you do iteration in foreach loop where you do match string like 'RestartNumber -le 1' of if block and then gets line number and insert '<#' before the linenumber and eventually using same logic to get the line number of end of the if block and insert '#>' after the linenumber. This way the second if block will be commented out and will not be executed during execution of powershell script The purpose is simple comment the if block which you do not want to get it execute but not manually instead through powershell script.
Thanks
Vivek
LainRobertson
Jul 27, 2022Silver Contributor
Yes, you can do it that way.
That's the basically the same as the second option I mentioned, just using a different (and harder to identify, in the case of the closing curly brace) set of "markers".
Both approaches will allow you to control which blocks you do/don't run.
Cheers,
Lain