SFTP using Powershell POSH-SSH

Install POSH-SSH module in PowerShell using command: REFERENCE

iex (New-Object Net.WebClient).DownloadString("https://gist.github.com/darkoperator/6152630/raw/c67de4f7cd780ba367cccbc2593f38d18ce6df89/instposhsshdev")

Example Output:



Import-Module Posh-SSH #Load the Posh-SSH module

#Set the credentials
$Password = ConvertTo-SecureString 'Password' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential ('user', $Password)

#Set local file path, SFTP path, and the backup location path which I assume is an SMB path
$FilePath = "C:\Sumit\SFTP.txt"
$SftpPath = '/home/user/SFTP-TEST/'

#Set the IP of the SFTP server
$SftpIp = ''

#Establish the SFTP connection
New-SFTPSession -ComputerName $SftpIp -Credential $Credential

#Upload the file to the SFTP path
Set-SFTPFile -SessionId 0 -LocalFile $FilePath -RemotePath $SftpPath

#Disconnect SFTP session
(Get-SFTPSession -SessionId 0).Disconnect()

Example script:


Another Example for authentication using Private Key:

# Define Serve Name
$ComputerName = ""

# Define UserName
$UserName = "user"

#Define the Private Key file path
$KeyFile = "C:\Users\sumit\MyPrivateKeyRSA\Key"
$nopasswd = new-object System.Security.SecureString

#Set Credetials to connect to server
$Credential = New-Object System.Management.Automation.PSCredential ($UserName, $nopasswd)

# Set local file path, SFTP path, and the backup location path which I assume is an SMB path
$FilePath = "C:\Sumit\SFTP1.txt"
$SftpPath = 'data/'

#Command to connect to the server for SFTP
#New-SSHSession -ComputerName $ComputerName -Credential $Crendtial -KeyFile $KeyFile

# Establish the SFTP connection
$SFTPSession = New-SFTPSession -ComputerName $ComputerName -Credential $Credential -KeyFile $KeyFile

# Upload the file to the SFTP path
Set-SFTPFile -SessionId $SFTPSession.SessionID -LocalFile $FilePath -RemotePath $SftpPath

# Disconnect SFTP session


Cleanup WinSXS folder in Server 2012 R2

LABOPS-0082The WinSxS folder contains the files for all the Windows Features you can install in the default operating system. Each time you run a windows update files in the WinSxS folder get update and the size will continue to grow.

In Windows Server 2012, A new feature called “Features on Demand” introduced. Instead of WinSxS containing all the binaries for all the features you could possibly install on the server, “Features on Demand” allows you to remove the files for features you aren’t using.

You can specify the source files location in case you want to install removed windows features.



Here in the screenshot, the folder size is 7.19GB.



Open PowerShell, and run the command Get-WindowsFeature to list all the features with their “Install State”.


The command to uninstall windows feature is:

Uninstall-WindowsFeature -Name “featurename” -Remove

With this command we can remove windows features one by one. Since we have list of features that can be uninstalled we can combine few commands as:

Get-WindowsFeature | Where-Object -FilterScript {$_.Installed -Eq $FALSE} | Uninstall-WindowsFeature -Remove



Now run the following command to verify the uninstallation of windows features

Get-WindowsFeature | WhereObject -FilterScript {$_.Installed -Eq $FALSE}


Also lets take a look on WinSxS folder size:


Check WMI Problem on SQL Cluster server

On SQL cluster, sometimes the fialover cluster manager stopped working because of WMI issue and we receive following errors:





But we cannot say its defining the WMI problem.. We can check by running below command on each node of the Cluster.

Get-WmiObject -namespace "rootmscluster" -class MSCluster_Resource

Also, there is a script taken from the Technet  The script can be run on one of the nodes that will connect to all the other nodes and check to see if the namespace is present.  If it is, it will succeed.  If the namespace does not exist, it will fail.

Set-ExecutionPolicy unrestricted


If(import-module failoverclusters)

Write-Host "Imported Cluster module"


Write-Host "Getting the cluster nodes..." -NoNewline
$nodes = Get-ClusterNode
Write-host "Found the below nodes "
Write-host " "
Write-host ""
Write-host "Running the WMI query...."
Write-host " "
ForEach ($Node in $nodes)
Write-Host -NoNewline $node

if($Node.State -eq "Down")

Write-Host -ForegroundColor White    " : Node down skipping"



$result = (get-wmiobject -class "MSCluster_CLUSTER" -namespace "rootMSCluster" -authentication PacketPrivacy               -computername $Node -erroraction stop).__SERVER
Write-host -ForegroundColor Green      " : WMI query succeeded "


Write-host -ForegroundColor Red -NoNewline  " : WMI Query failed "
Write-host  "//"$_.Exception.Message


In the below example, you can see that one of the nodes failed.



To correct the problem, you would need to run the below from an administrative command prompt on the “failed” node(s).

cd c:windowssystem32wbem
mofcomp.exe cluswmi.mof

Once the Cluster WMI has been added back, you can successfully open Failover Cluster Management.  There is no restart of the machine or the Cluster Service needed.

If nothing works, Then proceed to restart the server is not a bad option.

List all servers having IIS Role installed

Below is the VBS script for listing all the servers who have IIS roles installed (Server names are from a text file)

The script is












Save the code as .vbs file. Script is also attached in this post.

Create a Text file with name Computer.txt and Enter the server names inside. Example:






Open the command prompt and run the following command:




The result will be as example:








The script is Applicable for Server 2003/2008/2012 including R2..

​Thank you 🙂 download the attached script from  link: Find IIS roles on all servers


Forest Preparation Failed Lync 2013

While i was installing Lync 2013 on server 2012 R2 i got an error on the step of Forest preparation as given in the screenshot:



To resolve this error i opened Lync powershell from start menu and ran this command:

Enable-CsAdForest -GroupDomain domain.local -GlobalSettingsDomainController dc.domain.local -GroupDomainController dc.domain.local -GlobalCatalog dc.domain.local

After running this command try to run the setup again and it will be fine this time.

Test SMTP using Telnet command

Telnet into server hosting SMTP feature installed using TCP port 25.

c:telnet <servername> 25

Enter the following smtp command sequence

helo <your server name><enter>

response should be as follows
250 OK

mail from: <your Email Address><enter>

response should be:
250 OK – mail from <your Email address>

rcpt to: <recipient address><enter>

response should be:
250 OK – Recipient <recipient address>


response should be:
354 Send data.

End with CRLF.CRLF or put . (dot) to end

Subject: <Subject field of Email message><enter>
<Enter you body text><enter><enter> . <enter>
response should be:
250 OK


List Distribution Group members Exchange 2010/2013

Distribution Groups:

To check all commands related to Distribution groups

Get-Help *distrib*

To Get list of all distribution groups:


To get more details for specific Distribution list

Get-DistributionGroup <listname> | FL
Get-DistributionGroup <listname> | select Displayname, Primarysmtpaddress, Managedby

Now combine the Select Cmdlet with the ExpandProperty Cmdlet to see more information associated with the ManagedBy property.

Get-DistributionGroup <listname> | select Displayname, Primarysmtpaddress -ExpandProperty Managedby

Command to list the managers of all distribution groups.

Get-DistributionGroup  | select Displayname, Primarysmtpaddress -ExpandProperty Managedby | Select Displayname, Primarysmtpddress, name

To export to CSV command looks like this:

Get-DistributionGroup  | select Displayname, Primarysmtpaddress -ExpandProperty Managedby | Select Displayname, Primarysmtpddress, name | Export-CSV c:DGlist.csv

Distribution Group Members
Here now we will have commands to list all members of distribution group in exchange server.

The basic command

Get-DistributionGroup | Get-DistributionGroupMembers

more detailed results can be found as:

foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft alias, @{label=’Test1′;expression={$group.name}}}

and this command will give you all list of members in each distribution group.

foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft @{expression={$_.displayname};Label=”$group”}}

To output the data to a file, use the Out-File Cmdlet along with -append.

foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:tempDistributionListMembers.txt -append}

To sort alphabetically within each group add the sort displayname into the command as below:

foreach ($group in Get-DistributionGroup) { get-distributiongroupmember $group | sort displayname | ft @{expression={$_.displayname};Label=”$group”} | Out-File c:DGMembers.txt -append}


Also there is a script from Microsoft which do similar action in csv result. Here is the link to download that script