0

I'm having difficulties to join a text with 1 string how do I make this join without breaking line in power shell? can you help me.

I Want This:

> PlaybackDevice=AudioDevice 
> RingerDevice=AudioDevice
> RecordDevice=AudioDeviceRecord

But i have this on execute:

     PlaybackDevice= 
$audio.Name
     RingerDevice=  
$audio.Name 
     RecordDevice= 
$mic.Name

This is my code:

Add-Content "C:\burn.txt*" "RingerDevice=" $audio.Name 
Add-Content"C:\burn.txt*" "RecordDevice=" $mic.Name 
Add-Content "C:\burn.txt*" "PlaybackDevice=" $audio.Name

2 Answers 2

1

try this

Add-Content "C:\burn.txt*" "RingerDevice=$($audio.Name)"
Add-Content "C:\burn.txt*" "RecordDevice=$($mic.Name)"
Add-Content "C:\burn.txt*" "PlaybackDevice=$($audio.Name)"
Sign up to request clarification or add additional context in comments.

4 Comments

I can't be the only one that almost always resorts to something like ("PlaybackDevice=" + $audio.Name) in these situations simply bc the appearance of the string interpolation expression syntax is so jarring, hah. esp. for very short expressions
@diopside I've been there too!
You cannot have a "*" in a path.
Amazing! very thanks for this for helpfull!
0

"*" is not allowed in the a hard drive path. You must put ALL of content you want to write in "" or ''. Given that you have a variable such as "$audio.Name" it is treated as a object that has a label called "name" and a value called "logictech" in my example below. Add-Content only wants the value by using $() you can expand the variable and only give the value. e.g. $($audio.Name). Never write to the C:\ path. It is block in many places for security reasons. Run the code below for a working example.

$audio = [PSCustomObject]@{
    Name     = "logictech"
}

$mic= [PSCustomObject]@{
    Name     = "MyMic"
}

Add-Content -path "$env:temp\burn.txt" -Value "RingerDevice=$($audio.Name)"
Add-Content -Path "$env:temp\burn.txt" -Value  "RecordDevice=$($mic.Name)"
Add-Content -Path "$env:temp\burn.txt" "PlaybackDevice=$($audio.Name)"

sleep 10

Start-Process "$env:temp\burn.txt"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.