Partager via


PowerShell Command Separator Is ; and Not &

 

Ahh PowerShell, I still cannot fully embrace your powerful weirdness.

With normal NT command prompt I can restart my SQL Server by running =>

net stop mssqlserver & net start mssqlserver

This does not work in PowerShell, you get the usual cryptic, nonsensical and useless error message:

PS C:\> net stop mssqlserver & net start mssqlserver
Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
At line:1 char:23
+ net stop mssqlserver & <<<< net start mssqlserver
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed

Personally, I would change this error message to:

Ampersand not allowed. You can use the semi-colon instead for a command separator in PowerShell.

Ok, given this logic, then I try:

PS C:\> net stop mssqlserver "&" net start mssqlserver
The syntax of this command is:

NET STOP
service

Ok, I give up.  Time to search the internet.

Found some mention of an undocumented semi-colon as command separator, this works =>

PS C:\> net stop mssqlserver ; net start mssqlserver
The SQL Server (MSSQLSERVER) service is stopping.
The SQL Server (MSSQLSERVER) service was stopped successfully.

The SQL Server (MSSQLSERVER) service is starting.
The SQL Server (MSSQLSERVER) service was started successfully.

Comments

  • Anonymous
    April 28, 2012
    The comment has been removed

  • Anonymous
    April 30, 2012
    Does 'restart-service mssqlserver'  from an elevated powershell prompt not work for you?

  • Anonymous
    April 30, 2012
    Aha!  Thanks :)

  • Anonymous
    May 15, 2012
    Yes, of course I should learn the powershell secret command for re-starting the service! :)

  • Anonymous
    April 09, 2013
    It would be interesting to see how [net stop mssqlserver && net start mssqlserver ] will be translated to PS (two ampersands)?

  • Anonymous
    July 13, 2015
    The comment has been removed