Get User’s Desktop Path
$ShellFolders = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
$ShellFolders.Desktop
Details
Check the User Shell Folders registry keys for the path to the user’s desktop. This is better than using environmental variables as they may be using folder redirects.
  |  |  
Zip All Files in Folder
# Get the files to zip
$FilesToZip = Get-ChildItem -Path $FolderPath -File

# Load the assembly to get the zip functionality
[Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null
# Set compression level
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal

# Set Zip file pathc based on folder
$ZipPath = Join-Path $FolderPath "$(Split-Path $FolderPath -Leaf).zip"

# initialize the zip file
$Archive = [System.IO.Compression.ZipFile]::Open( $ZipPath, "Update" )

# add each file to the zip
Foreach($file in $FilesToZip){
    $AddZip = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Archive, $file.FullName, $file.Name, $compressionLevel)
}

# release the zip
$Archive.Dispose()
Write-Output "The file '$ZipPath' has been created"
Details
Creates a zip file and add every file from the specified directory into it. The zip file is named after the folder and will be placed inside of it.

Example
PS C:\> $FolderPath = "C:\Scripts\Logs"
>> $FilesToZip = Get-ChildItem -Path $FolderPath -File
>> [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) | Out-Null
>> $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
>> $ZipPath = Join-Path $FolderPath "$(Split-Path $FolderPath -Leaf).zip"
>> $Archive = [System.IO.Compression.ZipFile]::Open( $ZipPath, "Update" )
>> Foreach($file in $FilesToZip){
>>     $AddZip = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Archive, $file.FullName, $file.Name, $compressionLevel)
>> }
>> $Archive.Dispose()
>> Write-Output "The file '$ZipPath' has been created"

The file 'C:\Scripts\Logs\Logs.zip' has been created
  |  |  |  
Get Folder Path of the Current Script
Function Get-ScriptDirectory{
    $Invocation = (Get-Variable MyInvocation -Scope 1).Value;
    if($Invocation.PSScriptRoot){
        $Invocation.PSScriptRoot;
    }
    elseif($Invocation.MyCommand.Path){
        Split-Path $Invocation.MyCommand.Path
    }
    elseif($Invocation.InvocationName.LastIndexOf("\") -gt 0){
        $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\"));
    }
    else {
        Get-PSDrive | Where-Object{$_.Provider.Name -eq 'FileSystem'} | Foreach-Object {
            Join-Path -Path $_.Root -ChildPath $_.CurrentLocation
        }
    }
}
Details
Returns the path of the script being executed. If unavailable, it will return the path of the PowerShell console.

Example
PS C:\> Function Get-ScriptDirectory{
>>     $Invocation = (Get-Variable MyInvocation -Scope 1).Value;
>>     if($Invocation.PSScriptRoot){
>>         $Invocation.PSScriptRoot;
>>     }
>>     elseif($Invocation.MyCommand.Path){
>>         Split-Path $Invocation.MyCommand.Path
>>     }
>>     elseif($Invocation.InvocationName.LastIndexOf("\") -gt 0){
>>         $Invocation.InvocationName.Substring(0,$Invocation.InvocationName.LastIndexOf("\"));
>>     }
>>     else {
>>         Get-PSDrive | Where-Object{$_.Provider.Name -eq 'FileSystem'} | Foreach-Object {
>>             Join-Path -Path $_.Root -ChildPath $_.CurrentLocation
>>         }
>>     }
>> }
>> Get-ScriptDirectory

C:\Scripts
  
Grant Permissions to Folder Share
Grant-SmbShareAccess -name $ShareName -AccountName $Account -AccessRight Full -Force
Details
Grants share permissions to a user or group

Example
PS C:\> $ShareName = 'ScriptFiles


>> $Account = 'Domain Admins'
>> Grant-SmbShareAccess -name $ShareName -AccountName $Account -AccessRight Full -Force


Name                    ScopeName               AccountName             AccessControlType       AccessRight
----                    ---------               -----------             -----------------       -----------
ScriptFiles$            *                       DOWST\Domain Admins     Allow                   Full

  |  |  
Revoke All Permissions From Folder Share
Get-SmbShareAccess -name $ShareName | Foreach {Revoke-SmbShareAccess -name $ShareName  -AccountName $_.AccountName -Force}
Details
Gets all the permissions for the share, then revokes them.

Example
PS C:\> $ShareName='ScriptFiles

>> Get-SmbShareAccess -name $ShareName | Foreach {Revoke-SmbShareAccess -name $ShareName  -AccountName $_.AccountName -Force}


Name                    ScopeName               AccountName             AccessControlType       AccessRight
----                    ---------               -----------             -----------------       -----------
ScriptFiles$            *                       NT AUTHORITY\SYSTEM     Allow                   Full
ScriptFiles$            *                       *S-1-5-5-0-158796760    Allow                   Read
ScriptFiles$            *                       *S-1-5-5-0-158796760    Allow                   Read
ScriptFiles$            *                       Everyone                Deny                    Full

  |  |  
Create a Folder Share
$Shares=[WMICLASS]'WIN32_Share'
$sd = ([WMIClass] "Win32_SecurityDescriptor").CreateInstance() 
$shares.create($FolderPath, $ShareName, 0, 100, "Description", "", $sd)
Details
Return all the domain and forest level FSMO roles.

Example
PS C:\> $FolderPath = 'C:\PSScripts\Share'
>> $ShareName='ScriptFiles$'
>> $Shares=[WMICLASS]'WIN32_Share'
>> $sd = ([WMIClass] "Win32_SecurityDescriptor").CreateInstance()
>> $shares.create($FolderPath, $ShareName, 0, 100, "Description", "", $sd)



__GENUS          : 2
__CLASS          : __PARAMETERS
__SUPERCLASS     :
__DYNASTY        : __PARAMETERS
__RELPATH        :
__PROPERTY_COUNT : 1
__DERIVATION     : {}
__SERVER         :
__NAMESPACE      :
__PATH           :
ReturnValue      : 0
PSComputerName   :


  |  
Create Folder If It Doesn’t Exist
If (-not(test-path $FolderPath)){
    New-Item -type directory -Path $FolderPath
}
Details
Check if a folder path exists using the Test-Path and if it doesn’t create the folder using New-Item

Example
PS C:\> $FolderPath = "C:\PSScripts"
>> If (-not(test-path $FolderPath)){
>>     New-Item -type directory -Path $FolderPath
>> }