repeat script options RRS feed

  • Question

  • I'm a powershell nube that needs some help.

    I have a script that works, basically its creating network locations in windows 10 to replace mapped drives.

    It uses two variables, one for the name of the location, and one for the UNC path of the target.

    I would like to create a single script that runs through this process several times with different values for the two variables each time.

    Is there a more efficient way to do this beyond just copy/paste the entire process and changing the variable values?  some way to repeat but with different values?

        $networkshortcut_name = $Name
        $networkshortcut_target = $Target
        $networkshortcuts_path = [Environment]::GetFolderPath('NetworkShortcuts')
        $networkshortcut_path = "$networkshortcuts_path\$networkshortcut_name"
        $desktopini_path = "$networkshortcut_path\desktop.ini"
        $targetlnk_path = "$networkshortcut_path\target.lnk"
        $desktopini_text = "[.ShellClassInfo]`r`nCLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}`r`nFlags=2"
        if( Test-Path -Path $networkshortcut_path -PathType Container )
            Remove-Item -Path $networkshortcut_path -Recurse -Force
        [void](New-Item -Path $networkshortcut_path -ItemType directory)
        Set-ItemProperty -Path $networkshortcut_path -Name Attributes -Value 1
        Out-File -FilePath $desktopini_path -InputObject $desktopini_text -Encoding ascii
        Set-ItemProperty -Path $desktopini_path -Name Attributes -Value 6
        $WshShell = New-Object -com WScript.Shell
        $Shortcut = $WshShell.CreateShortcut($targetlnk_path)
        $Shortcut.TargetPath = $networkshortcut_target

    any help would be appreciated

    Wednesday, October 3, 2018 4:12 PM

All replies

  • Use a loop.  I recommend that you learn PowerShell before trying to guess at how to do things.  Basic PowerShell knowledge is required before attempting to ask technical questions.

    1. Learn PowerShell  
    2. PowerShell Documentation
    3. PowerShell Style Guidelines


    Wednesday, October 3, 2018 5:17 PM
  • Build a CSV file that has two columns. One column called "Name" and the other called "Target". Fill the CSV with the appropriate values.

    Replace the first four of you lines of code with example below, and add a "}" at the end, like this:

    Import-CSV "<apriopriate-path-name>" | Foreach {
        $networkshortcut_name = $_.Name
        $networkshortcut_target = $_.Target
        .  the rest of your code

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, October 3, 2018 6:50 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    Best Regards,


    Just do it.

    Friday, October 26, 2018 8:42 AM
  • Hi,

    As this thread has been quiet for a while, we will mark it as ‘Answered’ as the information provided should be helpful. If you need further help, please feel free to reply this post directly so we will be notified to follow it up. You can also choose to unmark the answer as you wish.

    Best Regards,


    Just do it.

    Monday, November 5, 2018 9:22 AM