Creating SMB Shares Remotely

Here are a couple of lines that remotely create an SMB share on a server:

#requires -Version 3.0 -Modules CimCmdlets, SmbShare -RunAsAdministrator
$computername = 'Server12'
$shareName = 'ScriptExchange'
$fullAccess = 'domaingroupName'

$session = New-CimSession -ComputerName $computername
New-SMBShare -Name $shareName -Path c:Scripts -FullAccess $fullAccess -CimSession $session
Remove-CimSession -CimSession $session

You can then map a network drive to that share from your client. Note that network shares are per user, so when you map it with your Administrator account, it is not accessible in Windows Explorer.

$computername = 'Server12'
$shareName = 'ScriptExchange'
net use * "\$computername$shareName"

Twitter This Tip! ReTweet this Tip!

Important PowerShell Variables

Here is a list of important PowerShell variables: $pshome is the path to the place where PowerShell lives. $home is the path to your personal profile folder. And $PSVersionTable returns the PowerShell version and the versions of important subcomponents:

 
PS> $pshome
C:WindowsSystem32WindowsPowerShellv1.0

PS> $HOME
C:Userstweltner

PS> $PSVersionTable

Name                           Value                                                                                          
----                           -----                                                                                          
PSVersion                      5.1.14393.0                                                                                    
PSEdition                      Desktop                                                                                        
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                        
BuildVersion                   10.0.14393.0                                                                                   
CLRVersion                     4.0.30319.42000                                                                                
WSManStackVersion              3.0                                                                                            
PSRemotingProtocolVersion      2.3                                                                                            
SerializationVersion           1.1.0.1
 

$profile is the path to your personal autostart script that gets loaded automatically whenever your current PowerShell hosts starts (provided the file exists). $profile.CurrentUserAllHosts is the profile script that is loaded with any host. And $env:PSModulePath lists the folders where PowerShell modules can be stored that are auto-discoverable for PowerShell:

 
PS> $profile
C:UserstweltnerDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1

PS> $profile.CurrentUserAllHosts
C:UserstweltnerDocumentsWindowsPowerShellprofile.ps1

PS> $env:PSModulePath -split ';'
C:UserstweltnerDocumentsWindowsPowerShellModules
C:Program FilesWindowsPowerShellModules
C:WINDOWSsystem32WindowsPowerShellv1.0Modules

PS>
 

Twitter This Tip! ReTweet this Tip!

Read-Host Blocks Automation

Using Read-Host to ask for user information can be problematic because it prevents scripts from running unattended. A better way could be to wrap Read-Host in the param() block. This way, the information can be submitted via arguments for unattended operation, and prompted for interactive usage:

param
(
    $Name = $(Read-Host -Prompt 'Enter your name'),
    $Id = $(Read-Host -Prompt 'Enter your ID')
)


"You are $Name and your ID is $Id"

When you run above script, it prompts you with all the freedom Read-Host provides to design a prompt text. You can however also run the script with parameters:

 
PS> C:myscript.ps1 –Name test –Id 12 
 

If you do not need custom prompting, you can go even simpler, and declare parameters as mandatory by adding [Parameter(Mandatory)] above each parameter variable.

Twitter This Tip! ReTweet this Tip!

Mapping Network Drives

PowerShell offers numerous ways to connect to SMB file shares. Here are three different approaches:

# adjust path to point to your file share
$UNCPath = '\servershare'

net use * $UNCPath 
New-PSDrive -Name y -PSProvider FileSystem -Root $UNCPath -Persist
New-SmbMapping -LocalPath 'x:' -RemotePath  $UNCPath

Net.exe is the most versatile approach and available in all PowerShell versions. By providing a “*”, it automatically picks the next available drive letter.

New-PSDrive supports SMB shares beginning in PowerShell 3. New-SmbMapping requires the SmbShare module and seems to be a bit buggy at this time: the drive shows in Windows Explorer only after a reboot.

Twitter This Tip! ReTweet this Tip!

Safely Deleting Data

To safely delete files, folders, or entire drives, PowerShell can use the built-in cipher.exe tool. This line would safely delete the old user profile:

Cipher.exe /w:c:UsersObsoleteUser

Note that the path to the folder to delete needs to be separated by a *** from the parameter /w. Clearing the data takes a while: Windows is overwriting the entire data content multiple times to ensure it cannot be recovered.

Twitter This Tip! ReTweet this Tip!