Powershell/WScript Create Shortcut if not exist

889 views Asked by At

I'm wondering if someone can help me? I've butchered a few powershell scripts I've found online that make shortcuts from $source to $destination. However, it appears to overwrite each time, and I only want it to create a .lnk on new.

The original source of the script is here and this is my current "non working" script.. I added the following, but it doesn't seem to work. I think I need to somehow get it to check the $destination and then continue if $file.lnk doesn't exist

If ($status -eq $false) {($WshShell.fso.FileExists("$Destination") + "*.lnk")

Full script:

    function Create-ShortcutForEachFile {

    Param(
        [ValidateNotNullOrEmpty()][string]$Source,
        [ValidateNotNullOrEmpty()][string]$Destination,
        [switch]$Recurse
    )

    # set recurse if present
    if ($Recurse.IsPresent) { $splat = @{ Recurse = $true } }

    # Getting all the source files and source folder
    $gci = gci $Source @splat
    $Files = $gci | ? { !$_.PSisContainer }
    $Folders = $gci | ? { $_.PsisContainer }

    # Creating all the folders
    if (!(Test-Path $Destination)) { mkdir $Destination -ea SilentlyContinue > $null }
    $Folders | % {
        $Target = $_.FullName -replace [regex]::escape($Source), $Destination
        mkdir $Target -ea SilentlyContinue > $null
    }

    # Creating Wscript object
    $WshShell = New-Object -comObject WScript.Shell

    # Creating all the Links
    If ($status -eq $false) {($WshShell.fso.FileExists("$Destination") + "*.lnk")

      $Files | % {
          $InkName = "{0}.lnk" -f $_.sBaseName
          $Target = ($_.DirectoryName -replace [regex]::escape($Source), $Destination) + "\" + $InkName
          $Shortcut = $WshShell.CreateShortcut($Target)
          $Shortcut.TargetPath = $_.FullName
          $Shortcut.Save()
          }
        }
}
Create-ShortcutForEachFile -Source \\myserver.domain.local\Folder1\Folder2\Test -Destination \\myserver2.domain.local\Folder1\Folder2\Test -Recurse

Hoping anyone can help me out, apologies for being a powershell/scripting noob.

1

There are 1 answers

0
Martin Adams On

My brother kindly reworked the script to suit better to my needs.

Here it is:

    #################################################
<#
    CREATE-SHORTCUT - creates shortcut for all files from a source folder
    version       : 1.0
    Author        : 
    Creation Date : 
    Modified Date : 
#>

#------------------------------------------------------------[ variables ]----------------------------------------------------------

$sourceDir="D:\scripts\create-shortcut\source"
$targetDir="D:\scripts\create-shortcut\dest"

#-------------------------------------------------------------[ Script ]-----------------------------------------------------------
# get files/files from folder
$src_gci=Get-Childitem -path $sourceDir -Recurse
$src_files=$src_gci | ? { !$_.PSisContainer }
$src_folders=$src_gci | ? { $_.PSisContainer }

# create subfolders
$src_folders | Copy-Item -Destination { join-path $targetDir $_.Parent.FullName.Substring($sourceDir.Length) } -Force

# create shortcuts
$WshShell = New-Object -comObject WScript.Shell
$src_files | % {
    $lnkName="{0}.lnk" -f $_.BaseName
    $Target = ($_.DirectoryName -replace [regex]::escape($sourceDir), $targetDir) + "\" + $lnkName
    $Shortcut = $WshShell.CreateShortcut($Target)
    $Shortcut.TargetPath = $_.FullName
    $Shortcut.Save()
    # change to SourceFiles ModifiedDate #
    $src_date=$_.LastWriteTime
    Get-ChildItem $Target | % { $_.LastWriteTime = "$src_date" }
}