$LogonScriptsLocation = "\\$DomainFQDN\LogonScript\"


I'd like to share a ps1 which contains variables. How do I replace all values which are between ### with blanks via Powershell?

F.e. myvariables.ps1 contains
$Fileserver = 'MyFileserver'
$SmtpServer = 'MySMTPServer"
$LogonScriptsLocation = "\\$DomainFQDN\LogonScript\"

I could work via get-content ... | replace content but howto replace the " ..." or ' ...' by " " correctly via Powershell?

Thanks for your input.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Given that replacevars.ps1 contains

$myFileserver = 'MyFileserver'
$mySmtpServer = 'MySMTPServer'
$LogonScriptsLocation = '\\$DomainFQDN\LogonScript\'

Open in new window

Get-Content .\replacevars.ps1 | % {$_ -replace "^(\s*\$.*my.*\s*=\s*\').*(\')$", '$1$2'}

Open in new window

Would return

$myFileserver = ''
$mySmtpServer = ''
$LogonScriptsLocation = '\\$DomainFQDN\LogonScript\'

Open in new window

I believe you didn't want to blank all values, therefore I prefixed the "custom" variable names with my.

Hope that helps.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
janhoedtAuthor Commented:
Thanks! So how does it recognizes the ###? If I would replace it by something else like ### or ###variables###, would that ask a major change?
The script does not recognize the ### but it does recognize variables which name is prefixed with my such as $myVar1 $myVar2 where $Var3 wouldn't match.

I believe it achieves the same goal as you first described.

Does my explanation cleared it up for you ?

Another approach could be to have the variables in a separate file which is dot sourced and that you clean the variables only in that file.
SD-WAN: Making It Work for You

As bandwidth requirements and Internet costs grow, businesses naturally want to manage budgets by reducing reliance on their most expensive connection types. Learn more about how to make SD-WAN work for your business in our on-demand webinar!

Also, if you really want to use the ### block delimiter, i am pretty sure there is a way to do this with a loop going through the array returned by get-content looking for beggining marker changing values until it finds the end marker.
janhoedtAuthor Commented:
My variables do not start with my, it was just an example ....
janhoedtAuthor Commented:
What bout working with clear-variable?

$Test = 'Hello'
Clear-Variable test

If I could import the ps1, then clear the variables and write them back to another file. But how can I make sure only the variables are cleared from a specific file ...?
janhoedtAuthor Commented:
Hope below (content of my variables.ps1 file) clarifies what I'm doing (my variables.ps1 which is cleared out of  values for variables and which I can share publicly.
So how to remove them  by psscript, f.e.  $Global:Company = 'MyCompany'  should become  $Global:Company = '' etc

$Global:Company = ''
$Global:Organization = ''
$Global:SCCMSiteCode01 = ''
$Global:SCCMSiteServer01 = ''
$Global:PublicSMTPServer01 = 'SMTP' #SMTP server which does not need authentication
#Might also be variables which need = " "

$Global:DomainFQDN = $env:USERDNSDOMAIN
$Global:EmailSuffix = "@DomainFQDN"
$Global:MailFromAddress = "noreply$EmailSuffix"
$Global:MailToAddressDeploySoftware = "mymailaddress$EmailSuffix"

$Global:SCCMSiteServer01_FQDN =  ($SCCMSiteServer01 + '.' + $DomainFQDN)
$Global:PublicSMTPServer01_FQDN = ($PublicSMTPServer01 + '.' + $DomainFQDN)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.