easy script creation request...

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of Joseph Storm Jr. Joseph Storm Jr. 3 months, 3 weeks ago.

  • Author
    Posts
  • #49711
    Profile photo of Joseph Storm Jr.
    Joseph Storm Jr.
    Participant

    I need to delete a file from 5 servers in the following location. C:\Users\*USERNAMES*\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WorkInstructor – Shortcut
    this will be replaced with another program that is currently on the desktop of all the users that have logged into the servers.
    Also the shortcut is in the C:\Users\Public\Desktop\MES_Administration.jnlp
    if i can have the workinstructor removed and replaced with the MES_admin file that will be great!

  • #49715
    Profile photo of Don Jones
    Don Jones
    Keymaster

    OK... so, what have you tried, and where do you need some help?

  • #49717
    Profile photo of Joseph Storm Jr.
    Joseph Storm Jr.
    Participant

    i started with dos but i cannot find the syntax if it even exists to include all the users with the users folder. so i am guessing another language will need to be used that is robust. i do not know powershell as well and i chose powershell since its windows.

  • #49719
    Profile photo of Joseph Storm Jr.
    Joseph Storm Jr.
    Participant

    "del \\servername\C:\Users\*\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\shortcut" this was my attempt

  • #49721
    Profile photo of Don Jones
    Don Jones
    Keymaster

    So, there's probably a little Windows-ish stuff, too, that has nothing to do with PowerShell. \\servername\c: isn't a valid UNC path, for example. Most Windows machines will have the C: drive shared as C$, so \\servername\c$\users\whatever... would work, if you're an administrator. And, in Windows, it isn't legal to use a wildcard (*) as part of a file path the way you have. Instead, you'd recurse the available directories.

    So the goal is to delete this specified shortcut from all Users' folders on the computer? Do you know if the C$ share is available to you?

  • #49723
    Profile photo of Joseph Storm Jr.
    Joseph Storm Jr.
    Participant

    i can remote into \\computername\c$

  • #49727
    Profile photo of Don Jones
    Don Jones
    Keymaster

    OK, so there's the easy-to-understand way, which will be incredibly slow, and the harder way, which won't be. I suppose the faster way makes more sense.

    You'll need to start by getting a list of user folders, since you can't use a wildcard.

    Get-ChildItem -Path \\server\c$\users -Directory

    The -Directory switch won't work earlier than PowerShell 3, but it keeps you from getting any files.

    Get-ChildItem -Path \\server\c$\users -Directory |
    ForEach-Object {
      $full_path = "\\server\c$\$($_.Name)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\WorkInstructor – Shortcut"
      Remove-Item $full_path
    }
    

    Within the {script block} of ForEach-Object, $_ represents whatever was piped in. In this case, it was a folder that was piped in, so $_ will be that folder. The folder's Name property will be the... well, the name of the folder. So I'm just mashing that folder name into the full UNC path of what you're trying to delete, and then trying to delete it.

    Make sense? Hopefully that gives you a starting point to work from. Not being in your environment I can't guarantee it'll work, but hopefully you can tweak it as needed from there. Good luck!

  • #49729
    Profile photo of Don Jones
    Don Jones
    Keymaster

    And I want to make sure you know what all this means, rather than just copying and pasting it, so if something isn't clear, please ask so that I can explain it.

  • #49731
    Profile photo of Don Jones
    Don Jones
    Keymaster

    (and I should point out that we don't do "script creation requests" here – we're volunteers, and most have our own jobs to do, and so creating full scripts for other people is not usually practical. Our goal is to try and answer questions and get you pointed in the right direction, where we can – hopefully, the above will help do that in this case.)

  • #49735
    Profile photo of Wilfredo Perez
    Wilfredo Perez
    Participant

    I would suggest getting a PowerShell book like Learn Windows PowerShell in a Month of Lunches and going over to https://mva.microsoft.com. Like Don said, we are here to help. Use the get-help within PowerShell, it's a great way to learn.

  • #49739
    Profile photo of Joseph Storm Jr.
    Joseph Storm Jr.
    Participant

    thank you guys for the learning experience. I will invest in the book for learning.

You must be logged in to reply to this topic.