0

I am trying to write a script that writes PowerShell profiles. This is helpful for new users who want a profile that will automate what they are doing daily.

I am trying to figure out how I can take a large chunk of script with various objects and write it to a profile that was just made a few lines before.

The problem I am running into is that I am trying to turn a large block of script into a string that can then be written to that profile.

An example:

$text='"welcome, want to go to site?"

$goyn=read-host -prompt "enter y or n"

if($goyn -eq 'y'){
start 'www.google.ca'
}
else{"continue on your journey of awesomeness"}


Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Get-WmiObject -Class Win32_OperatingSystem -ComputerName localhost |
Select-Object -Property CSName,@{n="Last Booted";
e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}'

I have tried putting it between single quotes then passing it into a variable but to no avail.

3
  • Are you asking how to use the Set-Content and Add-Content cmdlets? Commented Aug 11, 2016 at 22:08
  • 1
    It's completely unclear to me what you're asking here. What do you want to write where? How? Do you want to create some kind of persistent history for the PowerShell console? Commented Aug 11, 2016 at 22:56
  • Maybe you are looking for here-strings? Commented Aug 11, 2016 at 23:44

2 Answers 2

1

It's not completely clear what you're asking but it sounds like you're having trouble with quoting. You can use here-strings as Patrick Meinecke suggested. That looks like this:

$text=@'
"welcome, want to go to site?"

$goyn=read-host -prompt "enter y or n"

if($goyn -eq 'y'){
start 'www.google.ca'
}
else{"continue on your journey of awesomeness"}


Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Get-WmiObject -Class Win32_OperatingSystem -ComputerName localhost |
Select-Object -Property CSName,@{n="Last Booted";
e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}
'@

Another thing you can do is just use a [ScriptBlock] naturally. This also gives you the benefit of syntax highlighting and tab completion in your editor.

$sb = {
    "welcome, want to go to site?"

    $goyn=read-host -prompt "enter y or n"

    if($goyn -eq 'y'){
    start 'www.google.ca'
    }
    else{"continue on your journey of awesomeness"}


    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    Get-WmiObject -Class Win32_OperatingSystem -ComputerName localhost |
    Select-Object -Property CSName,@{n="Last Booted";
    e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}
}

You can pass the script block directly to something like Set-Content:

Set-Content -Path C:\my\profile.ps1 -Value $sb

But if you want to be sure you're getting a string (maybe not all cmdlets are as forgiving), just call .ToString() on the script block:

$text = $sb.ToString()
Sign up to request clarification or add additional context in comments.

1 Comment

here-string didn't work but used the script block and ToString method and worked great! Thanks!
1

As Patrick Meinecke suggested in the comments, it sounds like here-strings are what you want.

Here-Strings make it easier to create a string of text with multiple quote levels, multiple lines, etc. without having to do a bunch of escaping and end up with a messy looking bit of code.

If you want your text saved just like you wrote it out, you would do it like this:

$text = @'
"welcome, want to go to site?"

$goyn=read-host -prompt "enter y or n"

if($goyn -eq 'y'){
start 'www.google.ca'
}
else{"continue on your journey of awesomeness"}


Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
Get-WmiObject -Class Win32_OperatingSystem -ComputerName localhost |
Select-Object -Property CSName,@{n="Last Booted";
e={[Management.ManagementDateTimeConverter]::ToDateTime($_.LastBootUpTime)}}
'@

A here-string starts with @ and ends with @. If you use double-quotes, your variables will be expanded but if you use single-quotes (as in the example above), variables will not be expanded.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.