Windows CMD

Net Use

The command net use connects a computer to or disconnects a computer from a shared resource or displays information about computer connections. We can connect to a file share with the following command and map its content to the drive letter n.

net use n: \\\\10.10.10.10\\Finance /user:username Password

With the shared folder mapped as the n drive, we can execute Windows commands as if this shared folder is on our local computer. Let's find how many files the shared folder and its subdirectories contain.

dir n: /a-d /s /b | find /c ":\\"
Syntax Description
dir Application
n: Directory or drive to search
/a-d /a is the attribute and -d means not directories
/s Displays files in a specified directory and all subdirectories
/b Uses bare format (no heading information or summary)

The following command | find /c ":\\\\" process the output of dir n: /a-d /s /b to count how many files exist in the directory and subdirectories. You can use dir /? to see the full help. Searching through 29,302 files is time consuming, scripting and command line utilities can help us speed up the search.

dir n:\\*cred* /s /b

dir n:\\*secret* /s /b

Findstr

If we want to search for a specific word within a text file, we can use findstr.

We can find more findstr examples here.

findstr /s /i cred n:\\*.*

Windows Powershell

PowerShell was designed to extend the capabilities of the Command shell to run PowerShell commands called cmdlets. Cmdlets are similar to Windows commands but provide a more extensible scripting language. We can run both Windows commands and PowerShell cmdlets in PowerShell, but the Command shell can only run Windows commands and not PowerShell cmdlets.

Get-ChildItem \\\\192.168.220.129\\Finance\\

#Instead of net use, we can use New-PSDrive in PowerShell.
New-PSDrive -Name "N" -Root "\\\\192.168.220.129\\Finance" -PSProvider "FileSystem"

To provide a username and password with Powershell, we need to create a PSCredential object. It offers a centralized way to manage usernames, passwords, and credentials.

$username = 'plaintext'
$password = 'Password123'
$secpassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential $username, $secpassword
New-PSDrive -Name "N" -Root "\\\\192.168.220.129\\Finance" -PSProvider "FileSystem" -Credential $cred

GCI

In PowerShell, we can use the command Get-ChildItem or the short variant gci instead of the command dir.