First, the cmdlet you often need to use is Get-Help. This command will provide help with commands and examples.
This command provides a short and long description of the command itself, as you can see below.
Also, if you need examples, you can run using the examples parameter.
The following example shows examples and help for the Get-SqlDatabase cmdlet:
Get-Help Get-SqlDatabase -examples
If you want to go to SQLPS just write sqlps in the command line:
In addition, you can go to SQL using the dir or ls commands. Write ls. Ls or dir are used to list files or directories. In PowerShell, the dir, ls, or get-child items lits the contained items in the current item.
The ls will show you the physical folders and files. If you want to go to SQL server, write this:
If you write ls or dir here you will see all the items of SQL Server like they were folders.
You can use the cd to change the directory and go to different items of the database. You can go to the databases section with these commands:
Where server name is the SQL Server name and the instance name is the name of the SQL Server instance whose name default by default. Finally, we will work with the SQL cmdlets.
Working with SQL Cmdlets
Firstly, in the database node, we will run the Get-SqlDatabase cmdlet. This will list the databases, the names, status, recovery model, compatibility level, owner, and Containment Type.
Also, if you want to get the information of a single database, you can use the database name parameter. The following example will show the information of the msdb system database.
Get-SqlDatabase -Name "msdb" the cmdlet to get database names
In addition, we have the Invoke-Sqlcmd cmdlet. This cmdlet allows using T-SQL commands in PowerShell. Here is an example:
Invoke-Sqlcmd -Query "sp_who" execute SQL command
In the previous example, we executed the sp_who system procedure using PowerShell.
Finally, the next example will run the SELECT GETDATE() command:
Invoke-Sqlcmd -Query "SELECT GETDATE() AS servertime" Conclusion
To conclude we can say that Azure Data Studio provides a nice extension to run PowerShell. Also, we learned how to install it and use it. How to add terminals and we also applied some commands. PowerShell is a powerful shell that can help us a lot to automate our daily tasks. Finally, if you want to learn more about PowerShell, I strongly recommend reading our