# Powershell Basic , Beginner needs help

**URL:** https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153
**Category:** PowerShell Help
**Created:** [March 1, 2017, 3:54am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153 "2017-03-01T03:54:42Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![randompowershelluser](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@randompowershelluser](https://forums.powershell.org/u/randompowershelluser)
#### Post date: [March 1, 2017, 3:54am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/1 "2017-03-01T03:54:42Z")

</div>

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 😃

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 1, 2017, 5:08am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/2 "2017-03-01T05:08:32Z")

</div>

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](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/test-path?f=255&MSPPError=-2147217396)

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

[Copy-Item](https://msdn.microsoft.com/en-us/powershell/reference/4.0/microsoft.powershell.management/copy-item)

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

[Get-Content](https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.management/get-content)[Select-String](https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.utility/select-string) or [-match a reguklar expression](https://technet.microsoft.com/de-de/library/2007.11.powershell.aspx)

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 1, 2017, 5:09am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/3 "2017-03-01T05:09:22Z")

</div>

… 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](https://blogs.technet.microsoft.com/heyscriptingguy/2011/03/21/use-powershell-to-replace-text-in-strings/)

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

[Add-Content](https://msdn.microsoft.com/en-us/powershell/reference/3.0/microsoft.powershell.management/add-content?f=255&MSPPError=-2147217396)

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

[Out-File](https://msdn.microsoft.com/en-us/powershell/reference/5.0/microsoft.powershell.utility/out-file) Here you can find information about how to run code conditionally [about\_comparison\_operators](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_comparison_operators) In general you should read about [get-help](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/get-help?f=255&MSPPError=-2147217396) and [get-command](https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/get-command)

Have a lot of fun!

---

<div class="post-metadata">

### Author: ![randompowershelluser](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@randompowershelluser](https://forums.powershell.org/u/randompowershelluser)
#### Post date: [March 1, 2017, 7:24am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/4 "2017-03-01T07:24:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)
#### Post date: [March 1, 2017, 9:01am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/5 "2017-03-01T09:01:39Z")

</div>

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"
}
```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 1, 2017, 9:13am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/6 "2017-03-01T09:13:02Z")

</div>

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”
}
```

---

<div class="post-metadata">

### Author: ![randompowershelluser](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@randompowershelluser](https://forums.powershell.org/u/randompowershelluser)
#### Post date: [March 1, 2017, 9:14am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/7 "2017-03-01T09:14:43Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 1, 2017, 9:33am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/8 "2017-03-01T09:33:04Z")

</div>

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  
}
```

---

<div class="post-metadata">

### Author: ![curtis-smith](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/curtis-smith/32/121_2.png) [@curtis-smith](https://forums.powershell.org/u/curtis-smith)
#### Post date: [March 1, 2017, 1:35pm UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/9 "2017-03-01T13:35:53Z")

</div>

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
}
```

---

<div class="post-metadata">

### Author: ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)
#### Post date: [March 1, 2017, 4:44pm UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/10 "2017-03-01T16:44:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![randompowershelluser](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@randompowershelluser](https://forums.powershell.org/u/randompowershelluser)
#### Post date: [March 2, 2017, 4:51am UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/11 "2017-03-02T04:51:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/powershell-basic-beginner-needs-help/8153/12 "2024-05-16T20:39:11Z")

</div>


