I have a powershell script that does a Invoke-Sqlcmd and when the script completes it leaves me in 'SQLSERVER:>'.
Is there a way to tell my script to return me to original directory when it completes?
Import-Module SQLPS -DisableNameChecking | Out-Null
Push-Location "SQLSERVER:\sql\$Server\DEFAULT\databases\$database" | Out-Null
Invoke-Sqlcmd -Query $query
#More Work
pop-location
Alternatively you could push the location of the database you want to run queries/operations onto the location stack. Do work. Then pop the location off the location stack when done.
$Server : Being the server the db is being hosted on $database : The database name in your database server
The Out-Null isn't required. It just prevents output being pumped into the pipeline