locked
Basic script to delete with stored variable in path RRS feed

  • Question

  • Hi, I can´t get this to work and endless googling just turns up irrelevant results:

    $Members = get-ADGroupMember -Identity TempTesting
    $Members | ForEach-Object
        {
        Remove-Item -Path "\\Server\Share\$($._SamAccountName)\folder1\folder2\file.txt" -WhatIf -Force
        }

    I just get:

    cmdlet ForEach-Object at command pipeline position 1
    Supply values for the following parameters:
    Process[0]:

    Remove-Item -Path "\\Server\Share\$($._SamAccountName)\folder1\folder2\file.txt" -WhatIf -Force


    And it doesnt do anything. Obviously the objective is to get users from a group and for those members only, delete a file in a path that includes their username.

    But how do I get their username into the path of the ForEach loop?

    Thanks!




    • Edited by Trana010 Friday, April 3, 2020 10:34 AM
    Friday, April 3, 2020 10:32 AM

Answers

  • You have some syntax errors and some typos in your code ... 
    Get-ADGroupMember -Identity TempTesting | 
        ForEach-Object {
            Remove-Item -Path "\\Server\Share\$($_.SamAccountName)\folder1\folder2\file.txt" -WhatIf -Force
        }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Friday, April 3, 2020 10:55 AM
    • Marked as answer by Trana010 Friday, April 3, 2020 11:54 AM
    Friday, April 3, 2020 10:53 AM

All replies

  • You have some syntax errors and some typos in your code ... 
    Get-ADGroupMember -Identity TempTesting | 
        ForEach-Object {
            Remove-Item -Path "\\Server\Share\$($_.SamAccountName)\folder1\folder2\file.txt" -WhatIf -Force
        }


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Friday, April 3, 2020 10:55 AM
    • Marked as answer by Trana010 Friday, April 3, 2020 11:54 AM
    Friday, April 3, 2020 10:53 AM
  • Thanks to both of you! I´m sure your VBscript would have worked, but to start out with powershell and then change to something else would be admitting defeat, unthinkable! 
    Friday, April 3, 2020 11:56 AM