複数行の PowerShell スクリプトの貼り付けは PowerShell ISE では機能しますが、PowerShell コンソールでは失敗します

複数行の PowerShell スクリプトの貼り付けは PowerShell ISE では機能しますが、PowerShell コンソールでは失敗します

複数行のスクリプトを PowerShell ISE に貼り付けると、スクリプト ブロック全体が一度に実行されるようです。

PowerShell ISEからの出力

PS C:\> $PSVersionTable

Name                           Value                                                                                                                                                                                                                             
----                           -----                                                                                                                                                                                                                             
PSVersion                      5.1.18362.145                                                                                                                                                                                                                     
PSEdition                      Desktop                                                                                                                                                                                                                           
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                                                                                                           
BuildVersion                   10.0.18362.145                                                                                                                                                                                                                    
CLRVersion                     4.0.30319.42000                                                                                                                                                                                                                   
WSManStackVersion              3.0                                                                                                                                                                                                                               
PSRemotingProtocolVersion      2.3                                                                                                                                                                                                                               
SerializationVersion           1.1.0.1                                                                                                                                                                                                                           



PS C:\> if ($true) {
 Write-Output "yes"
}
else {
 Write-Output "no"
}
yes

PS C:\> 

複数行のスクリプトを PowerShell コンソールに貼り付けると、行ごとに実行しようとするようです。

PowerShell コンソールからの出力

C:\> $PSVersionTable                                                                                                    
Name                           Value
----                           -----
PSVersion                      5.1.18362.145
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18362.145
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


C:\> if ($true) {
>>  Write-Output "yes"
>> }                                                                                                                    yes
C:\> else {
>>  Write-Output "no"
>> }                                                                                                                    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.
At line:1 char:1
+ else {
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

C:\>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

PowerShell コンソールに期待する動作は PowerShell ISE での動作と同じであり、それがステートメントelseの一部であることを認識している必要がありますif

これまでそのように動作したことは覚えていません。この動作が異なる原因は何でしょうか?

答え1

else {が上の行とは異なる行にあるため、スクリプトが失敗しています}。次の行に広がっていることを示すバックティックを追加するか、 の前のキャリッジ リターンを削除してくださいelse

if ($true) {
  Write-Output "yes"
} else {
  Write-Output "no"
}

関連情報