I have a text file that has something like this:
Year= YYYY
Month= MM
I then read the file in, select the line I want, split the line so I only get what is to the right of the "=" sign, trim blank spaces, and put it all into a variable with this code:
$Year=Get-Content -Path .\test.txt | Where-Object {$_ -match 'Year='}
$Year=($Year -Split 'Year=').trim()
The above code is repeated for each setting I have (i.e. Year, Month, Day, Time).
When I put $Year into the command prompt I get "YYYY" (No leading space); but when I use Write-Host I get " YYYY" (has a leading space).
It is important that I be able to put $Year $Month next to eathother without a space for filepath and file naming reasons. (i.e. .\YYYY\MM or YYYYMM.txt).
I have tried:
Write-Host "Example: $Year\$Month"
Write-Host "Example: " $Year "\" $Month
Write-Host ("Example: $($Year)\$($Month)")
Write-Host "Example: $Year\" + $Month"
Write-Host "Example: $Year\" -f $Month"
....and several ways of implementing .trim() with no success.
If the problem is not in the Write-Host line, then the only other thing I can think of is that it must be with how the data is written into the variables. Please keep in mind, this is only an example, my actual problem is larger. This is just a simple example of what I need to accomplish.
QUICK UPDATE: I found a long workaround like this:
Write-Host "$Year\" -nonewline
Write-Host "$Month\".trim() -nonewline
Write-Host "$Day\".trim() -nonewline
BUT there has to be a better way to do this. I am using about 8 variables in my actual code. So to give 1 example of a filename and path would be 8 lines long...which would be dumb.