DomsFileSystemToolbox.psm1

<#
.Synopsis
   Searches a directory for a string in the containing files
.DESCRIPTION
   Searches a directory for a string in the containing files
.PARAMETER Value
   Specifies the search string
.PARAMETER Location
   Specifies the search directory. If empty, uses the current directory.
.PARAMETER Filter
   Specifies the file type filter.
.PARAMETER Recurse
   Specifies whether to search subdirectories or not.
.EXAMPLE
   Search-Directory -value "testVal" -location "C:\Location\" -recurse
.EXAMPLE
   Search-Directory -value "testVal" -location "C:\Location\" -filter *.csv -recurse
.INPUTS
   None
.OUTPUTS
   System.Management.Automation.PSCustomObject
.NOTES
   Yes. It is good.
#>


function Search-Directory{
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$value,
    [Parameter(Mandatory=$false,Position=2)]
    [string]$location,
    [Parameter(Mandatory=$false,Position=3)]
    [string]$filter,
    [Parameter(Mandatory=$false,Position=4)]
    [switch]$recurse
    )
    if($location -and -not(test-path $location)){
        return "Location value is not a valid path";
    }
    
    $results = "";
    
    if($location){
        "Searching for ""$value"" in directory $location";
    }else{
        "Searching for ""$value"" in current directory";
    }
    
    $results = dir -recurse:$recurse.IsPresent -path $location -filter $filter | sls -pattern $value | group path | select name
    
    if(-not($results)){
        "No results found";
    }else{
        return $results;
    }
}
New-Alias -Name 'Search' -Value 'Search-Directory'

<#
.Synopsis
   Creates a backup of a specified directory, and moves the backup to another specified location.
.DESCRIPTION
   Zips up a directory to another directory, with a given name. Adding ".zip" is inconsequential.
.PARAMETER Source
   Specifies the directory to be zipped.
.PARAMETER Destination
   Specifies the targed destination directory.
.PARAMETER ZipName
   Specifies the name of the resulting zip file.
.EXAMPLE
   New-Backup C:\Location\ C:\Backups\ TestZipName
.EXAMPLE
   Backup C:\Location\ C:\Backups\ TestZipName
.INPUTS
   None
.OUTPUTS
   None
.NOTES
   Yes. It is good.
#>

function New-Backup{
    #get-help about_functions_advanced_parameters
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$Source,

    [Parameter(Mandatory=$true,Position=2)]
    [string]$Destination,

    [Parameter(Mandatory=$true,Position=3)]
    [string]$ZipName
    )

    $Source = $Source.Trim('"')
    $Destination = $Destination.Trim('"')

    if(-not(test-path $Source)){return "Source is not a valid path"}
    if(-not(test-path $Destination)){return "Destination is not a valid path"}

    if(-not($ZipName -like "*.zip")){$ZipName = $ZipName + ".zip"}

    $zipFullName = ""

    if(($Destination -like "*\") -or ($Destination -like "*/")){$zipFullName = $Destination+""+$ZipName}
    else{$zipFullName = $Destination+"\"+$ZipName}

    if(test-path $zipFullName){
        echo "Removing file with the same name"
        remove-item $zipFullName
    }

    Compress-Archive -Path $Source -DestinationPath $zipFullName

    #echo "The directory has been successfully backed up"\
    $popupResult = (new-object -ComObject wscript.shell).Popup("The directory has been successfully backed up.",0,"Done")
}
New-Alias -Name 'Backup' -Value 'New-Backup'

<#
.Synopsis
   Extracts the contents of a zip file to a specified directory.
.DESCRIPTION
   Unzips a file at a specified path. If the file doesn't exist, or it is not a zip file, the script will alert the user.
.PARAMETER FilePath
   Specifies the location of the zip file to be extracted.
.PARAMETER Destination
   Specifies the target directory, where the zip file's contents will be extracted to.
.PARAMETER Force
   Specifies whether the target directory should be force created and extracted to.
.EXAMPLE
   Extract-ToDirectory C:\Location\zipFile.zip C:\Location\Extract\
.EXAMPLE
   Unzip C:\Location\zipFile.zip C:\Location\Extract\ -Force
.INPUTS
   None
.OUTPUTS
   None
.NOTES
   If the target directory doesn't exist, it will be automatically created.
#>

function Extract-ToDirectory{
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory=$true,Position=1)]
    [string]$FilePath,

    [Parameter(Mandatory=$true,Position=2)]
    [string]$Destination,
    
    [Parameter(Mandatory=$false,Position=3)]
    [switch]$Force
    )
    if(-not(test-path $FilePath) -or (-not([IO.Path]::GetExtension($FilePath) -eq ".zip" ))) {
        $fileName = $FilePath.Split("\")[-1]
        return "$fileName is not a valid zip file."
    }
    New-Item -Force:$Force -ItemType Directory -Path $Destination
    
    Expand-Archive $FilePath -DestinationPath $Destination -Force:$Force
}
New-Alias -Name 'Unzip' -Value 'Extract-ToDirectory'

<#
.Synopsis
   Opens any file, using the file's default program.
.DESCRIPTION
   Opens a file at a specified path. Doing so will automatically open the default program for the given file type. Will also open directories in file explorer.
.PARAMETER FilePath
   Specifies the location of the item to be invoked.
.EXAMPLE
   Open-File C:\Location\testNote.txt
.EXAMPLE
   Open C:\Location\
.INPUTS
   None
.OUTPUTS
   None
.NOTES
   Yes. It is good.
#>

function Open-File{
    [CmdletBinding()]
    param([string]$FilePath)
    if(test-path $FilePath){
        Invoke-Item $FilePath
    }else{
        Write-Host "The item at $FilePath does not exist. Please provide a valid path."
    }
}
New-Alias -Name 'Open' -Value 'Open-File'

Export-ModuleMember -Function * -Alias *