# carriage return disappears after string concatenation

**URL:** <https://forums.powershell.org/t/carriage-return-disappears-after-string-concatenation/4635>\
**Category:** PowerShell Help\
**Created:** [August 5, 2015, 3:00pm UTC](https://forums.powershell.org/t/carriage-return-disappears-after-string-concatenation/4635 "2015-08-05T15:00:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roustam-akhmetov](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/roustam-akhmetov/32/51_2.png) [@roustam-akhmetov](https://forums.powershell.org/u/roustam-akhmetov)\
**Post date:** [August 5, 2015, 3:00pm UTC](https://forums.powershell.org/t/carriage-return-disappears-after-string-concatenation/4635/1 "2015-08-05T15:00:03Z")

</div>

I am using this script to script out windows server logins:  
`$timestamp = Get-Date -Format yyyy-MM-dd
$Srv = ‘MyServer\MyInstance’
SL SQLSERVER:\SQL"$Srv"\Databases
$SQLInstance = New-Object “Microsoft.SqlServer.Management.Smo.Server” $Srv;
$SQLInstance.name
$BatchSeperator = “`r`nGO” #$null for none, or “`r`nGO” for carriage return, line feed and batch seperator.`

`
$so = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$so.IncludeDatabaseContext = $false;
$so.AllowSystemObjects = $false;
$so.IncludeDatabaseRoleMemberships = $true;
$so.IncludeIfNotExists = 1
$script = “USE [master]$BatchSeperator”
$WindowsLogins = $SQLInstance.Logins | Where-Object {@(“WindowsUser”,“WindowsGroup”) -contains $.LoginType} |% {$.Script($so)} |% {$_.ToString()+$BatchSeperator};
`

`$script = $script + “`r`n” + $WindowsLogins `

$WindowsLogins text has the correct structure:  
IF NOT EXISTS (SELECT \* FROM sys.server\_principals WHERE name = N’builtin\administrators’)  
CREATE LOGIN [builtin\administrators] FROM WINDOWS WITH DEFAULT\_DATABASE=[master], DEFAULT\_LANGUAGE=[us\_english]  
GO  
IF NOT EXISTS (SELECT \* FROM sys.server\_principals WHERE name = N’NT AUTHORITY\SYSTEM’)  
CREATE LOGIN [NT AUTHORITY\SYSTEM] FROM WINDOWS WITH DEFAULT\_DATABASE=[master], DEFAULT\_LANGUAGE=[us\_english]  
GO

However after I concatenate it to $script variable:  
$script = $script + “`r`n” + $WindowsLogins  
I get this:  
USE [master]  
GO  
IF NOT EXISTS (SELECT \* FROM sys.server\_principals WHERE name = N’builtin\administrators’)  
CREATE LOGIN [builtin\administrators] FROM WINDOWS WITH DEFAULT\_DATABASE=[master], DEFAULT\_LANGUAGE=[us\_english]  
**GO IF NOT** EXISTS (SELECT \* FROM sys.server\_principals WHERE name = N’NT AUTHORITY\SYSTEM’)  
CREATE LOGIN [NT AUTHORITY\SYSTEM] FROM WINDOWS WITH DEFAULT\_DATABASE=[master], DEFAULT\_LANGUAGE=[us\_english]  
GO EXEC …

As you can see the carriage return dissapeared from the string before “IF”, this gives me a syntax error when I try to use this script.

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [August 6, 2015, 3:52pm UTC](https://forums.powershell.org/t/carriage-return-disappears-after-string-concatenation/4635/2 "2015-08-06T15:52:19Z")

</div>

Your $WindowsLogins variable will wind up containing an array of strings, instead of a single string, as written. When you expand an array in a string, it winds up separated by the default field separator (which is a space, unless you’ve changed the global $OFS variable.)

Here’s how I’d tweak your code to accomplish the desired effect:

```
$timestamp = Get-Date -Format yyyy-MM-dd
$Srv = 'MyServer\MyInstance'
SL SQLSERVER:\SQL\"$Srv"\Databases
$SQLInstance = New-Object "Microsoft.SqlServer.Management.Smo.Server" $Srv;
$SQLInstance.name

$BatchSeperator = "`r`nGO" #$null for none, or "`r`nGO" for carriage return, line feed and batch seperator.

$so = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$so.IncludeDatabaseContext = $false;
$so.AllowSystemObjects = $false;
$so.IncludeDatabaseRoleMemberships = $true;
$so.IncludeIfNotExists = 1

$commands = @(
    'USE [master]'
    $SQLInstance.Logins | Where-Object {@("WindowsUser","WindowsGroup") -contains $_.LoginType} |% {$_.Script($so)} |% {$_.ToString()}
)

$script = $commands -join $BatchSeperator
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:45pm UTC](https://forums.powershell.org/t/carriage-return-disappears-after-string-concatenation/4635/3 "2024-05-16T20:45:16Z")

</div>


