Why working directory is changing when executing Invoke-sqlcmd? Why working directory is changing when executing Invoke-sqlcmd? powershell powershell

Why working directory is changing when executing Invoke-sqlcmd?


This issue occurs as part of running the Add-pssnapin sqlserverprovidersnapin100 -ErrorAction SilentlyContinue section of your code at the start. I'm not sure why it does this but I know that when I run a similar command (Import-Module 'sqlps' -DisableNameChecking) on computers in our network running MS SQL 2012, it changes the filesystem location from whatever location to the SQLSERVER:> location - I assume this is because it expects you to begin using commands in the SQL Server instance.

To overcome this problem, you can do a Push-Location and Pop-Location command to move back to your original location, like so;

C:\Users\foo.bar> Push-LocationC:\Users\foo.bar> Add-pssnapin sqlserverprovidersnapin100 -ErrorAction SilentlyContinueSQLSERVER:\> Add-pssnapin sqlservercmdletsnapin100 -ErrorAction SilentlyContinueSQLSERVER:\> Pop-LocationC:\Users\foo.bar> _

Either that or overcome the issue by factoring in the change in directory in your commands.