Powershell Basic , Beginner needs help

This topic contains 10 replies, has 3 voices, and was last updated by  RandomPowershellUser 7 months, 2 weeks ago.

  • Author
    Posts
  • #65415

    Hey,

    i would like to change a txt File .
    can you help me find the right Commands for my actions

    First of all i check if the txt file is in the path he should be
    if its not there he should copy my file from a shared source.
    If it is there he should search in the txt file, if there is a string called "xxx"
    if there is a string called "xxx" i want him to change the string to "yyy"
    if there is a string called "zzz" i want him to change the string to "yyy"
    so 2 different scenarios he need to check if there is a txt file already.

    if there is no such string "xxx" or "zzz" i want him to add the string "yyy"

    after that he should save the changes to the txt file.
    I would be very happy if you can help me 😀

  • #65421

    Olaf Soyk
    Participant

    Would you like us to do it for you or do you like to do it by yourself? 😉

    First of all i check if the txt file is in the path he should be

    Test-Path

    if its not there he should copy my file from a shared source.

    Copy-Item

    If it is there he should search in the txt file, if there is a string called "xxx"

    Get-Content
    Select-String or -match a reguklar expression

  • #65424

    Olaf Soyk
    Participant

    ... unfortunately I had to devide my post in two parts ...

    if there is a string called "xxx" i want him to change the string to "yyy"
    if there is a string called "zzz" i want him to change the string to "yyy"
    so 2 different scenarios he need to check if there is a txt file already.

    -replace

    if there is no such string "xxx" or "zzz" i want him to add the string "yyy"

    Add-Content

    after that he should save the changes to the txt file.

    Out-File
    Here you can find information about how to run code conditionally about_comparison_operators
    In general you should read about get-help and get-command

    Have a lot of fun!

  • #65430

    hey , thx vor the quick respond, i managed to do it by myself.
    I thought off placing a wildcard instead of using different scenarios to replace.

    I want the file to be checked so i can insert a string.
    the original content of the file is
    "defaultTheme=xminddefaultthemeid
    eclipse.preferences.version=1"

    i want to change the first row so the original string should be "defaulttheme=*" i want a * since the themecode can vary, and every themecode that is not "xxx" i want powershell to change for me.
    replacestring is defaultheme=xxx
    so if the string isnt already defaultheme=xxx i want him to change every other themecode after defaultheme= to mine....
    he wont do that unfortanetly

    Here is the full code to check on

    Set-StrictMode -Version "2.0"
    Clear-Host
    # PowerShell Checks If a File Exists
    $WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
    $FileExists = Test-Path $WantFile
    If ($FileExists -eq $True)
    {

    $Path="D:\Xmind\org.xmind.ui.mindmap.pref"
    #Professionell, Default
    $OriginalString="defaultTheme=*"
    #Klassisch 3
    $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
    #Auslesen
    $AllText = [IO.File]::ReadAllText($Path)
    #Ersetzen
    $AllTextNew=$AllText.Replace($OriginalString,$ReplaceString)
    #Ausgangsdatei ersetzen
    $AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"

    }

    If ($FileExists -eq $False)
    {New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
    }

    any tipps or suggestions

  • #65452

    Curtis Smith
    Participant

    See below. Comments made on changes to explain why.

    Set-StrictMode -Version "2.0"
    Clear-Host
    # PowerShell Checks If a File Exists
    $WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
    $FileExists = Test-Path $WantFile
    If ($FileExists -eq $True)
    {
    
    # Removed this because you already have your file path defined in $WantFile.
    # This is also the path you validated so it should be the one you use.
    #$Path="D:\Xmind\org.xmind.ui.mindmap.pref"
    
    #Professionell, Default
    # Changed this to a RegEx search pattern to be used with -replace
    $OriginalString="defaultTheme=.*"
    #Klassisch 3
    $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
    
    #Auslesen
    # Changed to use $WantFile since this path was validated
    # Changed to use Get-Content so that output file maintains its original formating
    #$AllText = [IO.File]::ReadAllText($Path)
    $AllText = Get-Content $WantFile
    
    #Ersetzen
    # Changed to use -replace which uses RegEx pattern matching to find the data to replace
    #$AllTextNew=$AllText.Replace($OriginalString,$ReplaceString)
    $AllTextNew=$AllText -Replace $OriginalString, $ReplaceString
    
    #Ausgangsdatei ersetzen
    $AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"
    
    }
    
    If ($FileExists -eq $False)
    {# Added the carriage return character to the value, `r, so that is shows up normally in notepad
     # with the values on different lines
    #New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
    New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `r`neclipse.preferences.version=1"
    }
  • #65460

    Olaf Soyk
    Participant

    So even a little bit more "streamliined" it could be like this:

    $WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
    If (Test-Path $WantFile){
        $OriginalString="defaultTheme=.*"
        $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
        $AllText = Get-Content $WantFile
        $AllTextNew=$AllText -Replace $OriginalString, $ReplaceString
        $AllTextNew | Out-File -FilePath "D:\Xmind\org.xmind.ui.mindmap.pref"
    }
    Else{
        New-Item D:\Xmind\org.xmind.ui.mindmap.pref -ItemType file -value "defaultTheme=23bcqg7bfvg262t8lg70ta186f `neclipse.preferences.version=1"
    }
  • #65461

    Thanks very much!
    In my Script it would have copied the replace string over and over again and it didnt accept the wildcard *
    So i would have 1 more scenario to solve
    for example the txt file is already in the folder but it doesnt have the string "defaultTheme=23bcqg7bfvg262t8lg70ta186f"
    there is only other information.
    i would like to add the string into the first row of the document.
    how can i easily manage that?

    The Problem with the different Defaulttheme entrys we solved by replacing but what can I do to check if there is any entry like that to then create on.

    Thanks in advance!

  • #65463

    Olaf Soyk
    Participant

    If I got you right ...

    $DefaultConfig = @"
    defaultTheme=23bcqg7bfvg262t8lg70ta186f
    eclipse.preferences.version=1
    "@
    
    $WantFile = "D:\Xmind\org.xmind.ui.mindmap.pref"
    If (Test-Path $WantFile){
        $CurrentContent = Get-Content $WantFile
        If($CurrentContent -match 'defaultTheme=23bcqg7bfvg262t8lg70ta186f'){
            $newContent = $CurrentContent | Foreach-Object {$_ -replace '(?< =defaultTheme=).*(?=$}','23bcqg7bfvg262t8lg70ta186f'}
            $newContent | Out-File -FilePath  $WantFile
        }
        Else{
            $newContent = $DefaultConfig + $CurrentContent
            $newContent | Out-File -FilePath  $WantFile
        }
    }
    Else{
        New-Item $WantFile -ItemType file -value $DefaultConfig
    }
  • #65482

    Curtis Smith
    Participant

    Olaf, you are also adding eclipse.preferences.version=1 which is not the desired results.

    $File = "D:\Xmind\org.xmind.ui.mindmap.pref"
    If (Test-Path $File)
    {
        $RegExPatt="defaultTheme=.*"
        $ReplaceString="defaultTheme=23bcqg7bfvg262t8lg70ta186f"
        $FileContent = Get-Content $File
        If ($FileContent -match $RegExPatt) {
            $FileContent -Replace $RegExPatt, $ReplaceString | Set-Content -Path $File
        } Else {
            "defaultTheme=23bcqg7bfvg262t8lg70ta186f",$FileContent | Set-Content -Path $File
        }
    } Else {
        "defaultTheme=23bcqg7bfvg262t8lg70ta186f","eclipse.preferences.version=1" | Set-Content -Path $File
    }
  • #65508

    Olaf Soyk
    Participant

    You're right. I wanted to avoid the backtick and ended up beyond the target. Thanks for correcting me. 😉

  • #65560

    Thanks a lot for your help guys.

    I would have 1 more Task to solve,

    i want to copy a file in specific folders
    the folder name can change due to different versions of the software.
    org.xmind.ui.resources_*VERSION*
    org.xmind.ui.resources_3.7.0.201612151837

    in every of this Folders org.xmind.ui.resources_*
    i want my file to be copied.
    Is there an easy way to make this possible
    Thanks in advance.

    Basically i want him to check all Folders that start like that, and copy a file into a subfolder
    org.xmind.ui.resources_*\templates\myfile

    so powershell should check for folders with that name to then copy my file into the template subfolder of those he found.

    i tried with get child item and includ templates.xml that way i can find all templates but thats the wrong way to go

You must be logged in to reply to this topic.