I have a question about the syntax in powershell for Add-Content. My problem is, I want to add text into a textfile and that this text contains "" which is not working. For example:
Add-Content -Value "c:\Users\Administrator\Desktop\"foobar.exe""
Now the output should look like this:
c:\Users\Administrator\Desktop\"foobar.exe"
and that does not work because of these "".
Is there a way to get these "" in the -value parameter?
You can do it like this
Add-Content -Value "c:\Users\Administrator\Desktop\`"foobar.exe`"" -Path .\AA.TXT
escaping the inner ""
Or using single quote to enclose the double quote
Add-Content -Value 'c:\Users\Administrator\Desktop\"foobar.exe"' -Path .\AA.TXT
You can use a few different tricks:
Include the doubly quoted items within a singly quoted string. This makes PowerShell ignore variables, too. For instance: 'I Said "Something"'
Include the doubly quoted items with a preceding backtick. Backticks are the escape character in PowerShell, like \ in javascript or C, and this will make the " part of the string.
Use a here document . They start like this: #" and must end with a "# at the start of a line. This will still let you use variables within the quotes, and still let regular double quotes be.
Related
I'm writing a quick Powershell script to import modules and update some default parameters on various machines. I'm running into an issue where in my script when I add $PSDefaultParameterValues to the $profile it changes to System.Management.Automation.DefaultParameterDictionary which then throws an error of not being recognized as the name of a cmdlet.
Here is the code in my ps1 script
Add-Content -Path $PROFILE -Value "$PSDefaultParameterValues = #{}"
Here is what gets added to the profile
System.Management.Automation.DefaultParameterDictionary = #{}
I've tried everything from using Set-Content to using variables to avoid quotation confusion.
I appreciate the help!
Use single quotes
Add-Content -Path $PROFILE -Value '$PSDefaultParameterValues = #{}'
These are literal strings so any variables will not expand.
To add to tonypags' helpful answer:
Double-quoted PowerShell strings ("...") are expandable strings, i.e they perform string interpolation of embedded variable references (e.g. $PSDefaultParameterValues) and subexpressions (e.g. $(1 + 2))
Single-quoted strings ('...') are verbatim strings, i.e. their content is used as-is (verbatim, literally).
Thus, the automatic $PSDefaultParameterValues variable was expanded in your "..." string, which in essence means it was replaced with its .ToString() representation - which in this case is simply the type name of the variable's value.
If your entire string is meant to be used verbatim, use '...' quoting, as shown in tonypags' answer.
If you do need expansions (string interpolation), but need to selectively suppress them, escape $ characters as `$, using ` (a backtick), PowerShell's escape character, as shown in the following example:
$enc = 'utf8'
# Note the backtick (`) before $PSDefaultParameterValues.
# NOTE: It is only the *outer* quoting that determines
# whether expansion is performed.
Add-Content -Path $PROFILE `
-Value "`$PSDefaultParameterValues = #{ '*:Encoding' = '$enc' }"
If I got a CSV file and there are a lot of strings that contain exactly "Name".
How do I get to have only Name and not "Name"? -replace won't work as you can't quote the quotes:
$computername.Name = $computername.Name -replace (""", "") # won't work
edit: example csv export Name,"IPAddress"
SVDCO03,"10.10.15.46"
SVLIC01,"10.10.20.221"
where i need to get those quotas away
not sure why u are trying to parse the csv you can actually quote the quotes :)
enclose the double quotes inside single and use the regex replace
"name" -replace '"'
or use the string replace method.
"name".replace('"','')
Okay someone suggested that I start a new question because my original question was solved but I have another question which belongs to my problem from before.
My first problem was that I wanted to write text in a txt file using double quotes. That is solved. My next problem / question is how can I work with more than one parameter in Add-Content -Value?
Here is an example:
Add-Content -Value '"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe" "-i C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt" "-o" "C:\Users\Administrator\Desktop\output_dcp\$title"'
In this case parameter $title stands for the title of the video clip I am working on and I do not know the title when I am working on it, that is why I am using this parameter. But when I am running my script, power-shell totally ignores my parameter. So I tried it again with single quotes around the parameter for example:
... "-i C:\Users\Administrator\Desktop\dcp_bearbeitet\'$title'\'$title'.txt" ...
And then power-shell does not even perform my script. So maybe somebody knows how I can work with parameters in Add-Content -Value?
You can pass arrays into Add-Content.
$string_array = #(
"`n",
"line1",
"line2",
"line3",
'"line4"',
"li`"ne`"5"
)
$string_array | Add-Content file.ext
The parameter -Value can also take an array directly:
Add-Content -path cake.txt -value #('some "stuff"',"more `"backticked`" stuff","hello world")
You have two competing requirements:
you want to substitute a variable into the command you are building so you need to use double quotes
you want to include double quotes in the output, so you either have to escape them or use single quotes round the string.
One way is to use a here-string, i.e. #"..."# to delimit the string instead of just using ordinary quotes:
$value = #"
"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe" -i "C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt" -o "C:\Users\Administrator\Desktop\output_dcp\$title"
"#
Add-Content -Value $value
That should work for you. I separated it out from Add-Content not because it wouldn't work in-place, but because there is less scope for confusion to do one thing at a time. Also if you build the value up separately you can add a temporary Write-Host $value to quickly check you have the string exactly as you want it.
Another way is simply to escape the enclosed quotes:
$value = "`"C:\Program Files (x86)\Fraunhofer IIS\easyDCP Creator+\bin\easyDCP Creator+.exe`" -i `"C:\Users\Administrator\Desktop\dcp_bearbeitet\$title\$title.txt`" -o `"C:\Users\Administrator\Desktop\output_dcp\$title`""
Add-Content -Value $value
but that does get messy.
Other options would be to build up the string in small chunks, or to use a different character instead of the double quotes inside the string and then do a replace to turn them into the quotes, but either of these is messy. I would go for the "here-string" solution.
I have a series of files that contain a string of characters such as this
(AEDOGRES)
An example file name would be
"Test 1(DOWKFUET).png"
I want to use a powershell command to get rid of the entire string in the parentheses to get something like this.
"Test 1.png"
I would expect to be able to use a command like this
dir | rename-item –NewName { $_.name –replace “(*)“,”” }
However, this is not working for me. Any help is greatly appreciated!
I am running powershell 3.0
Regex and wildcard matching have different rules. In regex, the * means "match zero or more of the previous character", and the parens are reserved characters used for grouping constructs and have to be escaped (using a backslash) if you want them to be used as a literal match.
See Get-Help about_regular_expressions, and try this:
$name = "Test 1(DOWKFUET).png"
$name -replace '\(.+\)',''
I am trying to use PowerShell do a simple find and replace. I use template text files and use $ in front of values that need to be changed.
Example:
(Get-Content "D:\test") | Foreach-Object {$_ -replace "`$TBQUAL", "DBO"} | Set-Content "D:\test"
It should find the line OWNER=$TBQUAL and make it look like OWNER=DBO.
I am using the escape in front of $TBQUAL with no luck. To test that it is working if I removed the $ from the front, it would replace TBQUAL and made it look like OWNER=$DBO.
Two things to get this to work:
Use single quotes for your strings so that the $ is not interpreted as the start of a variable to be expanded.
Escape the $ using a backslash "\" so the regular expression parser takes it literally.
For example,
PS C:\> 'Has a $sign in it' -replace 'a \$sign', 'no dollar sign'
Has no dollar sign in it
If you aren't using regular expressions in your replacement, you can do a simple (and fast) replace like this:
Foreach-Object {$_.Replace('$TBQUAL', 'DBO')}