Ohad Amar Ohad Amar - 1 year ago 135
PowerShell Question

Net view - get Just ' Share Name'

I need to get all of the shares name in some storage's.
Im using

Net view $StorageName
and it's show The result in a Table format :

Share name Type Used as Comment

Backups Disk
CallRecordings Disk
Download Disk System default share
home Disk Home
homes Disk System default share
Installs Disk
Justin Disk Copy of files from Justin laptop
michael Disk
Multimedia Disk System default share
Network Recycle Bin 1 Disk [RAID5 Disk Volume: Drive 1 2 3 4]
Public Disk System default share
Qsync Disk Qsync
Recordings Disk System default share
Sales Disk Sales Documents
SalesMechanix Disk
Server2012 Disk Windows Server 2012 Install Media
Usb Disk System default share
VMWareTemplates Disk
Web Disk System default share
The command completed successfully.

Thats good, but I need Just the Share Name.
I need Help please.
Thanke You!

Answer Source

Here is one way you could do it with the net view output:

(net view $StorageName | Where-Object { $_ -match '\sDisk\s' }) -replace '\s\s+', ',' | ForEach-Object{ ($_ -split ',')[0] }

Basically that is saying find the lines that have Disk surrounded by whitespace just in case something else might have Disk in the name. Then replace multiple spaces with a comma. Then, for each of those lines, split again by the comma and take the first value which would be the share name.

If you are on a Windows 8/2012 or newer system (and attempting to enumerate shares on other Windows systems), you could use a CIM session along with Get-SmbShare instead of net view which would return the results as objects and allow you to select the fields you want in the native PowerShell way.

For example:

$cimSession = New-CimSession $StorageName
Get-SmbShare -CimSession $cimSession | Select Name

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download