You can do it with following option:
Connect-SPOService -Url https://tenantname-admin.sharepoint.com -Credential [email protected]
after connection, you can pick one the below option:
Set-SPOTenant -SharingCapability ExternalUserAndGuestSharing
Set-SPOTenant -SharingCapability ExternalUserSharingOnly
Set-SPOTenant -SharingCapability Disabled
Managing Sharing and Access in SharePoint Online
But now there is catch, if external sharing is enabled and being used on many site collection:
If you turn off external sharing for your entire environment and later
turn it back on, external users who previously had access to content
or documents on sites will regain access to them. If you know that
external sharing was previously turned on and in use for specific site
collections and you do not want external users to be able to regain
access if external sharing is ever turned on again globally, we
recommend that you first turn off external sharing for those specific
site collections.
Source
Now you have to turn off the external sharing on the site collection where it was enabled, run below powershell to get that list:
Get-SPOSite | Select Url, SharingCapability
Now use the Set-SPoSite command to disable it.
Set-SPOSite -Identity ""Sitecolelction url" -SharingCapability Disabled