none
Newbie to Powershell - Create Directory and copy files RRS feed

  • Question

  • I am currently trying to do the following

    1. Create a new directory in c:\ProgramData\Microsoft\Windows
    2. Copy an icon file to that folder
    3. Copy a .lnk file to all users desktops

    If I run the create directory step separate in runs successfully

    If I run the copy steps separately they run

    When they are combined the script does not run?

     
    # Create TBIcons Folder
    $path= "C:\ProgramData\Microsoft\Windows\TBIcons"
    
    If(!(test-path $path))
    
    {
          New-Item -ItemType Directory -Force -Path $path
    }
    
    $UserFolders= Get-ChildItem C:\Users
    Foreach($UserFolder in $UserFolders)
    
    {
    Copy-Item ".\TBLearn.ico" -Destination "C:\ProgramData\Microsoft\Windows\TBIcons"-Force
    # Copy new shortcut
    Copy-Item ".\TrueBlue Learning Library.lnk" -Destination "C:\Users\$UserFolder\Desktop" -Force
    } 
    


    DHeinz

    • Moved by Bill_Stewart Monday, May 21, 2018 10:12 PM Use a GPO
    Tuesday, March 27, 2018 4:41 PM

All replies

  • Why use a script when you can use a GPO?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 27, 2018 4:42 PM
  • We are currently going through an AD migration and all GPOs are locked down.

    DHeinz

    Tuesday, March 27, 2018 4:47 PM
  • I would say that it is important to use the correct tool for the job.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, March 27, 2018 5:58 PM
  • A couple of things.

    When you say the script does not run what do you mean?

    I would move the copy of the TBLearn.ico file out of the loop so it only copies to the Windows folder once not once for every user.

    Tuesday, March 27, 2018 6:23 PM
  • Isn't $UserFolder an object? In your second Copy-Item statement, you're using it as a string.

    Keith


    Keith

    Tuesday, March 27, 2018 7:00 PM
  • I moved first Copy-Item command out of the loop. Also, instead of using $UserFolder.Name or $UserFolder.FullName consider using Join-Path:

    # Create TBIcons Folder
    $path= "C:\ProgramData\Microsoft\Windows\TBIcons"
    
    If(!(test-path $path))
    
    {
        New-Item -ItemType Directory -Force -Path $path
    }
    
    Copy-Item ".\TBLearn.ico" -Destination $path -Force
    
    $UserFolders= Get-ChildItem C:\Users
    Foreach($UserFolder in $UserFolders)
    {
        # Copy new shortcut
        Copy-Item ".\TrueBlue Learning Library.lnk" -Destination (Join-Path $UserFolder.FullName 'Desktop') -Force
    } 

    Friday, March 30, 2018 3:10 PM
  • To place a link on all users desktops just copy the link to the "Public Desktop" folder.  No need to place it in each individual folder.

    As noted above - a GPO is the easiest and most manageable way to do this.


    \_(ツ)_/

    Friday, March 30, 2018 3:13 PM
  • Try this and make sure you when you open powershell you open as admin!!!!!

    $path= "C:\ProgramData\Microsoft\Windows\TBIcons";If(test-path $path -PathType Container){write-host "$path already exists" -Foregroundcolor red} else {new-item -path $path -ItemType Directory};$UserFolders= Get-ChildItem C:\Users;Foreach($UserFolder in $UserFolders){Copy-Item ".\TBLearn.ico" -Destination "C:\ProgramData\Microsoft\Windows\TBIcons"-Force;Copy-Item ".\TrueBlue Learning Library.lnk" -Destination "C:\Users\$UserFolder\Desktop" -Force}

    But a GPO would be easier and quicker.


    Nick Hohn



    • Edited by HohnHT Friday, March 30, 2018 10:54 PM
    Friday, March 30, 2018 10:53 PM