{"id":7838,"date":"2014-10-16T06:00:00","date_gmt":"2014-10-16T13:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=7838"},"modified":"2014-10-13T16:10:34","modified_gmt":"2014-10-13T23:10:34","slug":"delete-desktop-icons-a-windows-powershell-tip","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/10\/16\/delete-desktop-icons-a-windows-powershell-tip\/","title":{"rendered":"Delete Desktop Icons: A Windows PowerShell Tip"},"content":{"rendered":"<p>I\u2019m a rather geeky type person with a passion for clean desktops. My physical desktop is always pretty clean and I like my computer desktops to be clean, too. I don\u2019t like anything on my desktop, including icons, especially on Windows 8.1, Server 2012 R2, and later, with their Start screen and taskbar. (I make an exception for the Recycle Bin, which really isn\u2019t an icon.) But habits die hard and some people like icons, so they\u2019re still popular.<\/p>\n<p>Automating the removal of icons from Windows 7 and earlier versions of Windows is particularly difficult. Most programs add their desktop icons to the All Users or Public desktop. To use Windows PowerShell to delete icons from desktops on Windows 7 and earlier, you need to take ownership of the All Users directory. That hardship seems to have been eliminated on Windows 8.1.<\/p>\n<p>To remove all icons from all Windows 8.1 desktops on a single computer:<\/p>\n<ol class=\"numbered\">\n<li>Start Windows PowerShell (or PowerShell Studio) with the <strong>Run as administrator<\/strong> option.<\/li>\n<li>Run this command:<\/li>\n<\/ol>\n<blockquote>\n<pre><span style=\"font-weight: bold; color: #0000ff;\">Remove-Item<\/span><span style=\"color: #000000;\"> C:\\Users\\*\\Desktop\\*lnk \u2013Force<\/span><\/pre>\n<\/blockquote>\n<p>The <strong>Force<\/strong> parameter is required on Windows 8.1 Pro and it doesn\u2019t do any harm on other systems.<\/p>\n<p>To remove all icons from a collection of local and remote computers in a Servers.txt file.<\/p>\n<blockquote>\n<pre><span style=\"font-weight: bold; color: #0000ff;\">Invoke-Command<\/span> <span style=\"color: #3399ff;\">-ComputerName<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"font-weight: bold; color: #0000ff;\">Get-Content<\/span><span style=\"color: #000000;\"> Servers.txt) `\r\n    <\/span><span style=\"color: #3399ff;\">-ScriptBlock<\/span><span style=\"color: #000000;\"> { <\/span><span style=\"font-weight: bold; color: #0000ff;\">Remove-Item<\/span><span style=\"color: #000000;\"> C:\\Users\\*\\Desktop\\*lnk <\/span><span style=\"color: #3399ff;\">-Force<\/span><span style=\"color: #000000;\"> }<\/span><\/pre>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>If you run this command frequently, create a function for it and add it to your Windows PowerShell profile. I also add a warning that reminds me when I\u2019m not running as an administrator. The cool isAdmin function is one of several versions of this function. This one was developed by Windows PowerShell MVP Shay Levy.<\/p>\n<p>Finally, because I\u2019m deleting something, I add the <strong>SupportsShouldProcess<\/strong> parameter of the CmdletBinding attribute and I make that link path a parameter (-Path), in case a user prefers to remove icons in only one user desktop.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #008000;\">#PowerShell MVP Shay Levy (@ShayLevy on Twitter)<\/span><br \/>\n<span style=\"color: #0000ff;\">function<\/span> <span style=\"color: #008080;\">isAdmin<\/span><span style=\"color: #000000;\"><br \/>\n{<br \/>\n([<\/span><span style=\"color: #0000cd;\">Security.Principal.WindowsPrincipal<\/span><span style=\"color: #000000;\">] `<br \/>\n[<\/span><span style=\"color: #0000cd;\">Security.Principal.WindowsIdentity<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #0000ff;\">::<\/span><span style=\"color: #000000;\">GetCurrent()).IsInRole(`<br \/>\n<\/span><span style=\"color: #000000;\">[<\/span><span style=\"color: #0000cd;\">Security.Principal.WindowsBuiltInRole<\/span><span style=\"color: #000000;\">]<\/span><span style=\"color: #ff0000;\">&#8220;Administrator&#8221;<\/span><span style=\"color: #000000;\">)<br \/>\n}<\/span><\/p>\n<p>&nbsp;<\/p>\n<pre><span style=\"color: #0000ff;\">function<\/span> <span style=\"color: #008080;\">Remove-Icons<\/span><span style=\"color: #000000;\">\r\n{\r\n    [<\/span><span style=\"color: #4682b4;\">CmdletBinding<\/span><span style=\"color: #000000;\">(SupportsShouldProcess <\/span><span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #8b0000;\">$true<\/span><span style=\"color: #000000;\">)]\r\n    <\/span><span style=\"color: #0000ff;\">param<\/span><span style=\"color: #000000;\">\r\n    (\r\n        [<\/span><span style=\"color: #0000cd;\">string<\/span><span style=\"color: #000000;\">]\r\n        <\/span><span style=\"color: #8b0000;\">$Path<\/span> <span style=\"color: #0000ff;\">=<\/span> <span style=\"color: #ff0000;\">\"C:\\Users\\*\\Desktop\\*lnk\"<\/span><span style=\"color: #000000;\">\r\n    )\r\n    <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #0000ff;\">!<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #696969;\">IsAdmin<\/span><span style=\"color: #000000;\">))\r\n    {\r\n        <\/span><span style=\"font-weight: bold; color: #0000ff;\">Write-Warning<\/span> <span style=\"color: #ff0000;\">\"To remove icons from all desktops, start \r\n                       Windows PowerShell with the 'Run as administrator' option.\"<\/span><span style=\"color: #000000;\">\r\n    }\r\n    \r\n    <\/span><span style=\"color: #0000ff;\">if<\/span><span style=\"color: #000000;\"> (<\/span><span style=\"color: #8b0000;\">$pscmdlet<\/span><span style=\"color: #000000;\">.ShouldProcess(<\/span><span style=\"color: #8b0000;\">$path<\/span><span style=\"color: #000000;\">))\r\n    {\r\n        <\/span><span style=\"font-weight: bold; color: #0000ff;\">Remove-Item<\/span> <span style=\"color: #8b0000;\">$Path<\/span> <span style=\"color: #3399ff;\">-Force<\/span><span style=\"color: #000000;\">\r\n    }\r\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Hope this helps to keep your desktop clean.<\/p>\n<p><i>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> or follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\">@juneb_get_help<\/a>.<\/i><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019m a rather geeky type person with a passion for clean desktops. My physical desktop is always pretty clean and I like my computer desktops to be clean, too. I don\u2019t like anything on my desktop, including icons, especially on Windows 8.1, Server 2012 R2, and later, with their Start screen and taskbar. (I make [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[303,25],"tags":[934,933],"class_list":["post-7838","post","type-post","status-publish","format-standard","hentry","category-tip","category-windows-powershell","tag-juneb","tag-powershell-tip"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=7838"}],"version-history":[{"count":13,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7838\/revisions"}],"predecessor-version":[{"id":7993,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/7838\/revisions\/7993"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=7838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=7838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=7838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}