Allow PowerShell formatting for "} else {"
System Details
PS > code -v 1.12.1 f6868fce3eeb16663840eb82123369dec6077a9b
PS > $pseditor.EditorServicesVersion
PS > code --list-extensions --show-versions
[email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected] [email protected]
PS > $PSVersionTable
Name Value
PSVersion 5.1.14409.1005 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.14409.1005 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
### Issue Description
The code formatter for if..else insists on the else begin on a line following the if closing bracket.
My desired behavior would be to have else occur on the same line as the closing bracket for the if statement as shown below:
if (condition) {
# do things
} else {
# do different things
}
The formatting for try...catch...finally DOES work in this manner.
I know this is closed but I am seeing issues with the if else formatting issue when use VScode. The following code executes without issue: if ($null -eq $LabsToProcess -or $selection -eq "Exit") { Write-Host 'Nothing to process' } Else { however if you right click and format document it changes the format to: if ($null -eq $LabsToProcess -or $selection -eq "Exit") { Write-Host 'Nothing to process' } Else { which results in: Else : The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
@skadlec72 how are you running the code? Are you copying and pasting into the terminal? What operating system are you on?
@bergmeister are you aware of a formatter setting that would give this "} else {" behavior?
@bergmeister I am running the code inside of VSCode terminal set to use windows powershell (not powershell core) and not the integrated terminal. I installed the powershell extension and use the right click option to for document format. I have executed the code by highlighting specific sections and also by saving it and running active file. The results are the same. Also filed as a bug in VSCode, but they pointed me to the extensions instead of product. I did just look and saw several options regarding newline with end brackets and more. The challenge I see is in some cases I want it do do a new line after the end bracket, just in this case it breaks.
Yes, this cuddled else style is the OTBS (one true brace style) setting of powershell.codeFormatting.preset which essentially is one of the 3 code styles (as a pssa settings file) that ship with pssa
https://github.com/PoshCode/PowerShellPracticeAndStyle/issues/81#issuecomment-285835313
Therefore this issue itself can be closed, I'll have a look at the user reported formatter behaviour if that is a bug
@skadlec72 I cannot reproduce the formatter issue, can you please supply your settings and exact text that you format as the text that you provided is missing a closing brace.
@bergmeister Here is sample code:
if ($null -ne $DomainCred) {
write-host "Already Exists"
} else {
$ParentFolder = New-RoyalObject -Folder $ParentFolder -Name $ResGroupName -Type RoyalFolder
$DomainCred = New-RoyalObject -type RoyalCredential -name $UserName -Folder $ParentFolder
Set-RoyalObjectValue -Object $DomainCred -Property "UserName" -Value $UserName
Set-RoyalObjectValue -Object $DomainCred -Property "Password" -Value $Password
}
I can reproduce this when I change the settings in the powershell extension. User setting:

Ironically it is doing exactly what the setting says, but for else it causes an issue, most other situations there are no issues.
@skadlec72 I cannot reproduce your issue with the stable PowerShell extension and "powershell.codeFormatting.newLineAfterCloseBrace": true,.
I've even tried to also set this setting "powershell.codeFormatting.preset": "OTBS",
Although I am using PowerShell Core, it should not make a difference because this code is written in C# anyway and the difference between full .net and .net core is unlikely to explain a difference in what is a logical decision of the code.
Please supply full details:
- VS-Code and PowerShell extension version
- OS
- PowerShell version
- All VS-Code settings
If I can repro afterwards, then we should open a new issue at https://github.com/PowerShell/PSScriptanalyzer
@TylerLeonhardt We will use this issue just for 1 or 2 conversations to boil down the problem but you can already close the issue because the issue is absout the feature request for OTBS, which has been delivered years ago