Q:

How to get primary replica info for the SQL availability group by powershell

I want to get the machine of current primary replica in Powershell. Is there cmdlet to do it?
I load sqlps module and dump the find all sql command, but seems no one is related to this..
Thanks

A:

I think this should work:
$null = [Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo");

$svr = New-Object Microsoft.SqlServer.Management.Smo.Server("AServerInstanceInYourAG");

$svr.AvailabilityGroups["YourAvailabilityGroup"].PrimaryReplicaServerName;

