What $ sign indicates in a folder path in powershell? - powershell

I have a powershell script that has a two $ signs like this :
$folderDest= '/Home/Vars/Cust/V1/Ex/Res/$toDo/$done'
what $toDo and $done stand for?
Note: I don't have these variables anywhere in the script.
Edit: The code below.
$month = Get-Date -Format yyyy-MM
$ftppath = $folderDest+ "/" + $month + "/*$xttz.txt"

$folderDest= '/Home/Vars/Cust/V1/Ex/Res/$toDo/$done'
In PowerShell the value of $folderDest will be interpreted as:
/Home/Vars/Cust/V1/Ex/Res/$toDo/$done
This is due to the single quotes around it as defined in this post.

Related

How to add a line to .txt file with special characters and variables in PowerShell

I have a PowerShell file e.g. C:\MyPowerShell.ps1 and I would like to have the following line there:
$myNewVariable = "Lukas"
Where string Lukas will be taken from variable $name. The variable $name will be declared before I run a command to do this action. I would like to use another PowerShell command to do that.
$name = "Lukas" <br>
Add-Content C:\MyPowerShell.txt ???
Please help me ;-)
Or use the -f Format operator:
$name = 'Lukas'
Add-Content -Path 'C:\MyPowerShell.txt' -Value ('$myNewVariable = "{0}"' -f $name)
Use an expandable (interpolating) string ("...") in which you individually `-escape $ characters in tokens you do not want to be expanded as variable references / subexpressions; similarly, escape embedded " characters as `" ("" would work too):
$name = 'Lucas'
Add-Content C:\MyPowerShell.txt -Value "`$myNewVariable = `"$name`""
Alternatively, use the -f operator, as shown in Theo's helpful answer.
This answer compares and contrasts these two approaches.

Using powershell to manipulate URL header with real live system data

I am trying to use powershell to manipulate a url value for a command program that is accessing a web Url.
Basically the case is like this
run in Powershell:
Program Argument https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g&sectime=1598048132
The Sectime is in Unix time format. What i am trying to do is :
use the powershell command (Get-Date -Date ((Get-Date).DateTime) -UFormat %s) to generate the system time in real time,
Append the result to the end of the Url as a value for sectime.
I tried to concatnate the arguments using call operator :
Arg1=program
Arg2=parameter
Arg3=Url
& $Arg1 Arg2 $Arg3
now the issue is i can do
this is the issue. & $Arg1 Arg2 $Arg3 and that works , but i can not modify the URL to take the real time command (Get- Date -Date ((Get-Date).DateTime) -UFormat %s)
if i split the URL arg into two i cant not concatnate them like this
"https://www.example.com/dash/926846/l.php?link=bTP77O5LyLorxCtjnkdE0g&sectime=(Get-Date -Date ((Get-Date).DateTime) -UFormat %s)."
Since it is a string and an command
An idea how to do this?
$uriTemplate = 'https://www.example.com/dash/{0}/l.php?link={1}&sectime={2}'
$id = '926846'
$link = 'bTP77O5LyLorxCtjnkdE0g'
$timeunixseconds = [System.DateTimeOffset]::UtcNow.ToUnixTimeSeconds().ToString()
$uriSting = [String]::Format($uriTemplate, # Template with placeholders {0}, {1}, {2}...
$id, # first argument goes {0}
$link, # second goes {1}
$timeunixseconds ) # third goes {2}, etc etc
$isUriOk = [System.Uri]::IsWellFormedUriString($uriSting, [System.UriKind]::Absolute)
DateTimeOffset class starting with some .net version (4.5th .net, 5th powershell, AFAIR) has To(From)UnixTimeSeconds and To(From)UnixTimeMilliseconds methods.
DateTime (and Get-Date, which returns DateTime) does not have such methods! Use DateTimeOffset.
And for your original case:
Inside double-quoted strings, variables (even simple, in my opinion) should be added in $() brackets:
"Hello, $($env:USERNAME), it's $(Get-Date -Format 'D') today!"
# Hello, User1, it's Aug 22 2020 today!
Single variables can be added without $() syntax, but I'd recommed add $() always.
Inside single-quoted strings, $() or $var does not work:
'Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!'
# Hello, $($env:USERNAME), it is $(Get-Date -Format "D") today!

Remove space between objects when using Write-Host

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.
The problem is that $Year -Split 'Year=' returns 2 elements, not just the string after Year=.
An immediate fix would be:
$Year = ($Year -Split 'Year=')[1].Trim()
More generally, you could write:
$Year = ($Year -split '=', 2)[1].Trim()
As for what you tried:
If the -split operator finds the separator string at the very start of the string too, it assumes that what precedes it is the first substring to return, which is the empty string in this case.
In your case, 'Year= YYYY' -split 'Year=' effectively yields array '', ' YYYY', and calling .Trim() on it trims the individual elements, so you end up with '', 'YYYY'.
If you print that to the console via PowerShell's implicit output, you can get an empty line, followed by YYYY, which is also what you would get if you used Write-Output $Year.
By contrast, if you use Write-Host, PowerShell stringifies the array by concatenating its elements with spaces (by default, can be changed with $OFS), so you got  YYYY.
To put it differently:
PS> Write-Host ('', 'YYYY')
YYYY # concatenation of: empty string, space, 'YYYY'
is by default implicitly the same as:
PS> Write-Host (('', 'YYYY') -join ' ')
YYYY

PowerShell - formatting date to yyMMdd format

I run a PowerShell script that passes a parameter, such as "20160428".
I need to format this into yyMMdd (i.e., getting "160428" as the result). I could just strip out the "20", but I would like to get this right. For such I did so many attempts, such as:
#$COBDATE= '{0:yyMMdd}' -f $COBDATE
#$COBDATE = ([datetime]::ParseExact($COBDATE, "yyMMdd", [System.Globalization.CultureInfo]::InvariantCulture )).DayOfWeek
And the last one:
$COBDATE = ("{0:yyMMdd}" -f [datetime]::parse($COBDATE))
The below works, but once I replace "Get-Date" by my date "20160428" it just prints out the yyMMdd string.
$b = (Get-Date).AddDays(-1).ToString("yyMMdd")
So if I try this:
$input = "20160428"
$format = "yyMMdd"
$input_toDate_up = [DateTime]::ParseExact($input, $format, $null).ToString($format)
$input_toDate_up
It just says that the string is not a valid Date Time, which seems to be the root cause.
How can I fix this?
$Input = "20160428"
Get-Date -Year $Input.Substring(0,4) -Month $Input.Substring(4,2) -Day $Input.Substring(6,2) -Format "yyMMdd"
I think for one $input is a reserved variable so you shouldn't use it as nothing will really work in there as you expect. See here about_Automatic_Variables
I have used this drawn out substring process before with varied abstract date formats.
$dateinput = "20160428"
$dateformat = "yyMMdd"
Get-Date($dateinput.Substring(4,2) + "/" + $dateinput.Substring(2,2) + "/" + $dateinput.substring(0,4)) -Format $dateformat
I'm sure there is a shorter regex method, but that is not under my hat yet.

Special characters in PowerShell

I am trying to work with PowerShell and have it output the copyright character into a Microsoft Word document. For example, the code listed below is what I am trying to use and it is not working.
$SummaryPara.Range.Text = "© "
$SummaryPara.Range.Text = "Get-Date -Format yyyy"
$SummaryPara.Range.Text = " - Name of Org Here "
$SummaryPara.Range.InsertParagraphAfter()
Do I need to use the Alt + 0169 sequence somehow?
I am not sure what I am doing wrong since the following code seems to work:
$selection.TypeParagraph()
$selection.TypeText("© ")
$selection.TypeText((Get-Date -Format yyyy))
$selection.TypeText(" - Name of Org Here ")
$selection.TypeParagraph()
How can I make this work for both the copyright character and other similar special characters?
There are a few problems here. I'll list those and address each:
You can get any character you need by casting the Unicode representation to a char. In this case
[char]0x00A9
You assign a new value to $SummaryPara.Range.Text three times. So, you are overwriting the previous value each time, instead of concatenating ('+' operator), which I think is what you were trying to do.
You are trying to use the cmdlet Get-Date, but since you have quoted it, you will end up with the literal string "Get-Date -Format yyyy", instead of the result of the cmdlet.
Putting it all together, I think you want something like this:
$word = New-Object -ComObject Word.Application
$doc = $word.Documents.Add()
$SummaryPara = $doc.Content.Paragraphs.Add()
$SummaryPara.Range.Text = [char]0x00A9 + ($date = Get-Date -Format yyyy) + " - Name of Org Here "
$word.Visible = $true