Search code examples
windowspowershellappdata

PowerShell script to remove file from hidden folders


I am currently trying to run a script I wrote. It works great, but I need it to also search and remove from the hidden folders as well. It does not seem to have any effect on hidden folders... Here is my script.

Get-ChildItem C:\ -Include saplogon.ini -Recurse | foreach ($_) {Remove-Item $_.fullname}

$src_dir = "\\xxxxxxxxxx\xxxxxxxxxxxx\saplogon\saplogon.ini"
$dst_dir = "C:\Windows"
$file = Get-ChildItem $src_dir
Copy-Item $file -Destination $dst_dir

[System.Environment]::SetEnvironmentVariable('SAP_LOGON_INI', 'C:\Windows\saplogon.ini', 'Machine')

Solution

  • You are missing the the -Force parameter. The code below is using alias so it won't require horizontal scrolling. Know that gci is Get-ChildItem.

    Note that you will only be able to get access if you have permission.

    gci c:\ -Include saplogon.ini -Recurse -Force | foreach ($_) {remove-item $_.fullname}
    

    At this point, you probably already took care of the non-hidden files. If you want to run the script again, but only for hidden files (and not non-hidden files), you can do that with the -Hidden flag.

    Again, you will only be able to get access if you have the permission.

    gci c:\ -Include saplogon.ini -Recurse -Hidden | foreach ($_) {remove-item $_.fullname}