Powershell command/script to change the content value to variable and to update in the deployment file

sns 9,246 Reputation points
2023-08-19T09:06:26.7633333+00:00

$variable1= it has got some value

and we have file called "deployment.txt"

is there any PowerShell command to change the content value to $variable1 in deployment.txt file?

and next it should read the value in the deployment.txt as a $variable2

and need to set the value of $variable to $buildfile

is there any PowerShell command to achieve it? Please suggest

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-08-19T14:51:09.84+00:00

    I don't understand your question, but I think that this might be what you are looking for.

    Given a deployment.txt file with this content...

    Name=xyzzy
    Folder=C:\Data
    Security=Users
    

    This PS code will change the folder value using regular expressions.

    $data = Get-Content -Path C:\temp\deployment.txt
    "Here is the data that we read in."
    $data
    $tagname = "Folder"      # This is the tag that we want to look for
    $value = "C:\Temp"       # This is the new value
    $find = "$tagname=(.*)"       # This is the regex string to find the tag and whatever value it has.
    $replace = "$tagname=$value"  # This is the replacement string. 
    $updated = $data -replace $find,$replace
    ""
    "Here is the updated content."
    $updated
    $updated | Set-Content -Path C:\temp\deployment.txt       # Now update the file. 
    

    User's image

    If you just want to overwrite the entire file, then pipe the variable to the set-content cmdlet, like I did in the last line of the script.


  2. MotoX80 36,291 Reputation points
    2023-08-20T12:53:48.22+00:00

    what are the amendments I should do?

    The basic problem is that I don't know anything about your environment or what data is in your deployment.txt file. So when I read your comment "change the content value to $variable1 in deployment.txt file", it leads me to believe that there are multiple lines in the file that contain some value. Like in an INI file that contains all of the settings for an application. That's a fairly common task.

    That is why my first reply demonstrated how to read in a file, look for some keyword, and modify it's value.

    The instruction to "Given that the buildfile value from your deployment.txt" would seem to support my assumption that deployment.txt contains multiple lines, and one of them might look like "BuildFile=SomeDataIsHere".

    If you just want to overwrite the entire file, that's simple.

    $variable1 = "Some data goes here."
    $variable1 | Set-Content -Path C:\temp\deployment.txt 
    

    From the literal instructions... but this does not make any sense to me.... there has to be something else...

    $variable1 = $build.sourcebranch                               # 1
    $variable1  | Set-Content -Path C:\temp\deployment.txt         # 2
    $AnotherVariable = Get-Content -Path C:\temp\deployment.txt    # 3
    $variable2  | Set-Content -Path C:\temp\deployment.txt         # 4
    $variable2 = $buildfile                                        # 5
    

    Here is a good Powershell reference that you should download and review.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.