Copy Files in the Powershell

Windows4Life 36 Reputation points
2021-11-01T22:58:28.573+00:00

Hello all. I have build a package in the SCCM that creates a shortcut when deployed. I am using powershell script to accomplish that.

Param(
     [parameter(Mandatory=$true)]
     [ValidateSet("ADD","REMOVE")]
     [String]$ShortcutType
   )

If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation } Else { $InvocationInfo = $MyInvocation }
$scriptDirectory = Split-Path -Path $InvocationInfo.MyCommand.Definition -Parent

Copy-Item $scriptDirectory\SupportFiles\atlas.ico -Destination C:\ProgramData\ -Recurse -Force

Function  Push-OneStreamWebAll{
$Shell1 = New-Object -ComObject ("WScript.Shell")
$ShortCut1 = $Shell1.CreateShortcut("C:\Users\Public\Desktop\Service Catalog.lnk")
$ShortCut1.TargetPath ="C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
$ShortCut1.Arguments = "https://Google.com"
$ShortCut1.WorkingDirectory = "C:\Program Files (x86)\Microsoft\Edge\Application"
$ShortCut1.WindowStyle = 1
$ShortCut1.IconLocation = "C:\ProgramData\atlas.ico"
$ShortCut1.Save()

}


Function Remove-OneStreamWebShortCuts{
$REMTYPE = $ShortcutType -Replace "REMOVE",""
Remove-Item "$Env:Public\Desktop\Service Catalog.lnk" -Force
}


if ($ShortcutType -eq "ADD")
{Push-OneStreamWebAll}

if ($ShortcutType -like "REMOVE*")
{Remove-OneStreamWebShortCuts}

Shortcut is being created. However script for some reason does not copy the file in to the proper directory.

Windows for business Windows Server User experience PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2021-11-02T14:21:53.32+00:00

    Have your script generate a transcript to log what it's doing.

     Param(
          [parameter(Mandatory=$true)]
          [ValidateSet("ADD","REMOVE")]
          [String]$ShortcutType
        )
    Start-Transcript -Path C:\Windows\Temp\MyScript.log
    
    ... the rest of your script.
    
    Stop-Transcript
    

    Add in additional Get-Childitem cmdlet's to get a list of the files and folders where you expect the source files to be.

    Examine the log for errors.

    1 person found this answer helpful.

  2. Rich Matheisen 47,901 Reputation points
    2021-11-02T18:36:28.94+00:00

    Remove the trailing "\" from "C:\ProgramData\" on line 10.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.