VirtualDesktop.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 |
# Author: Markus Scholtes, 2017/05/08 # Version 2.11 - support for Powershell Core, 2022/02/13 # prefer $PSVersionTable.BuildVersion to [Environment]::OSVersion.Version # since a wrong Windows version might be returned in RunSpaces if ($PSEdition -eq "Desktop") { # Windows Powershell $OSVer = $PSVersionTable.BuildVersion.Major $OSBuild = $PSVersionTable.BuildVersion.Build } else { # Powershell Core $OSVer = [Environment]::OSVersion.Version.Major $OSBuild = [Environment]::OSVersion.Version.Build } if ($OSVer -lt 10) { Write-Error "Windows 10 or above is required to run this script" exit -1 } if ($OSBuild -lt 14393) { Write-Error "Windows 10 1607 or above is required to run this script" exit -1 } Add-Type -Language CSharp -TypeDefinition @" using System; using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Text; // Based on http://stackoverflow.com/a/32417530, Windows 10 SDK, github project Grabacr07/VirtualDesktop and own research namespace VirtualDesktop { #region Type definitions // define HString on .Net 5 / overwrite HString on .Net 4 (Copyright (c) 2021 voed, https://github.com/voed/VirtualDesktop.Net5) [StructLayout(LayoutKind.Sequential)] public struct HString : IDisposable { private readonly IntPtr handle; public static HString FromString(string s) { var h = Marshal.AllocHGlobal(IntPtr.Size); Marshal.ThrowExceptionForHR(WindowsCreateString(s, s.Length, h)); return Marshal.PtrToStructure<HString>(h); } public void Delete() { WindowsDeleteString(handle); } [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", CallingConvention = CallingConvention.StdCall)] private static extern int WindowsCreateString([MarshalAs(UnmanagedType.LPWStr)] string sourceString, int length, [Out] IntPtr hstring); [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true)] private static extern int WindowsDeleteString(IntPtr hstring); [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", CallingConvention = CallingConvention.StdCall, ExactSpelling = true, CharSet = CharSet.Unicode)] private static extern IntPtr WindowsGetStringRawBuffer(HString hString, IntPtr length); public void Dispose() { Delete(); } public static implicit operator string(HString hString) { var str = Marshal.PtrToStringUni(WindowsGetStringRawBuffer(hString, IntPtr.Zero)); hString.Delete(); if (null != str) return str; else return string.Empty; } } #endregion #region COM API internal static class Guids { public static readonly Guid CLSID_ImmersiveShell = new Guid("C2F03A33-21F5-47FA-B4BB-156362A2F239"); public static readonly Guid CLSID_VirtualDesktopManagerInternal = new Guid("C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B"); public static readonly Guid CLSID_VirtualDesktopManager = new Guid("AA509086-5CA9-4C25-8F95-589D3C07B48A"); public static readonly Guid CLSID_VirtualDesktopPinnedApps = new Guid("B5A399E7-1C87-46B8-88E9-FC5747B171BD"); } [StructLayout(LayoutKind.Sequential)] internal struct Size { public int X; public int Y; } [StructLayout(LayoutKind.Sequential)] internal struct Rect { public int Left; public int Top; public int Right; public int Bottom; } internal enum APPLICATION_VIEW_CLOAK_TYPE : int { AVCT_NONE = 0, AVCT_DEFAULT = 1, AVCT_VIRTUAL_DESKTOP = 2 } internal enum APPLICATION_VIEW_COMPATIBILITY_POLICY : int { AVCP_NONE = 0, AVCP_SMALL_SCREEN = 1, AVCP_TABLET_SMALL_SCREEN = 2, AVCP_VERY_SMALL_SCREEN = 3, AVCP_HIGH_SCALE_FACTOR = 4 } [ComImport] $(if (($PSEdition -eq "Core") -Or ($OSBuild -lt 17134)) {@" // Windows 10 1607 and Server 2016 or Powershell Core: [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] "@ } else {@" // Windows 10 1803, up and Windows Powershell: [InterfaceType(ComInterfaceType.InterfaceIsIInspectable)] "@ }) $(if ($OSBuild -lt 17134) {@" // Windows 10 1607 and Server 2016: [Guid("9AC0B5C8-1484-4C5B-9533-4134A0F97CEA")] "@ }) $(if (($OSBuild -ge 17134) -And ($OSBuild -lt 17661)) {@" // Windows 10 1803: [Guid("871F602A-2B58-42B4-8C4B-6C43D642C06F")] "@ }) $(if ($OSBuild -ge 17661) {@" // Windows 10 1809 or up and Windows 11: [Guid("372E1D3B-38D3-42E4-A15B-8AB2B178F513")] "@ }) internal interface IApplicationView { int SetFocus(); int SwitchTo(); int TryInvokeBack(IntPtr /* IAsyncCallback* */ callback); int GetThumbnailWindow(out IntPtr hwnd); int GetMonitor(out IntPtr /* IImmersiveMonitor */ immersiveMonitor); int GetVisibility(out int visibility); int SetCloak(APPLICATION_VIEW_CLOAK_TYPE cloakType, int unknown); int GetPosition(ref Guid guid /* GUID for IApplicationViewPosition */, out IntPtr /* IApplicationViewPosition** */ position); int SetPosition(ref IntPtr /* IApplicationViewPosition* */ position); int InsertAfterWindow(IntPtr hwnd); int GetExtendedFramePosition(out Rect rect); int GetAppUserModelId([MarshalAs(UnmanagedType.LPWStr)] out string id); int SetAppUserModelId(string id); int IsEqualByAppUserModelId(string id, out int result); int GetViewState(out uint state); int SetViewState(uint state); int GetNeediness(out int neediness); int GetLastActivationTimestamp(out ulong timestamp); int SetLastActivationTimestamp(ulong timestamp); int GetVirtualDesktopId(out Guid guid); int SetVirtualDesktopId(ref Guid guid); int GetShowInSwitchers(out int flag); int SetShowInSwitchers(int flag); int GetScaleFactor(out int factor); int CanReceiveInput(out bool canReceiveInput); int GetCompatibilityPolicyType(out APPLICATION_VIEW_COMPATIBILITY_POLICY flags); int SetCompatibilityPolicyType(APPLICATION_VIEW_COMPATIBILITY_POLICY flags); $(if ($OSBuild -lt 17134) {@" int GetPositionPriority(out IntPtr /* IShellPositionerPriority** */ priority); int SetPositionPriority(IntPtr /* IShellPositionerPriority* */ priority); "@ }) int GetSizeConstraints(IntPtr /* IImmersiveMonitor* */ monitor, out Size size1, out Size size2); int GetSizeConstraintsForDpi(uint uint1, out Size size1, out Size size2); int SetSizeConstraintsForDpi(ref uint uint1, ref Size size1, ref Size size2); $(if ($OSBuild -lt 17134) {@" int QuerySizeConstraintsFromApp(); "@ }) int OnMinSizePreferencesUpdated(IntPtr hwnd); int ApplyOperation(IntPtr /* IApplicationViewOperation* */ operation); int IsTray(out bool isTray); int IsInHighZOrderBand(out bool isInHighZOrderBand); int IsSplashScreenPresented(out bool isSplashScreenPresented); int Flash(); int GetRootSwitchableOwner(out IApplicationView rootSwitchableOwner); int EnumerateOwnershipTree(out IObjectArray ownershipTree); int GetEnterpriseId([MarshalAs(UnmanagedType.LPWStr)] out string enterpriseId); int IsMirrored(out bool isMirrored); $(if (($OSBuild -ge 17134) -And ($OSBuild -lt 17661)) {@" int Unknown1(out int unknown); int Unknown2(out int unknown); int Unknown3(out int unknown); int Unknown4(out int unknown); "@ }) $(if ($OSBuild -ge 17661) {@" int Unknown1(out int unknown); int Unknown2(out int unknown); int Unknown3(out int unknown); int Unknown4(out int unknown); int Unknown5(out int unknown); int Unknown6(int unknown); int Unknown7(); int Unknown8(out int unknown); int Unknown9(int unknown); int Unknown10(int unknownX, int unknownY); int Unknown11(int unknown); int Unknown12(out Size size1); "@ }) } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] $(if ($OSBuild -lt 17134) {@" // Windows 10 1607 and Server 2016: [Guid("2C08ADF0-A386-4B35-9250-0FE183476FCC")] "@ }) $(if (($OSBuild -ge 17134) -And ($OSBuild -lt 17661)) {@" // Windows 10 1803: [Guid("2C08ADF0-A386-4B35-9250-0FE183476FCC")] "@ }) $(if ($OSBuild -ge 17661) {@" // Windows 10 1809 or up and Windows 11: [Guid("1841C6D7-4F9D-42C0-AF41-8747538F10E5")] "@ }) internal interface IApplicationViewCollection { int GetViews(out IObjectArray array); int GetViewsByZOrder(out IObjectArray array); int GetViewsByAppUserModelId(string id, out IObjectArray array); int GetViewForHwnd(IntPtr hwnd, out IApplicationView view); int GetViewForApplication(object application, out IApplicationView view); int GetViewForAppUserModelId(string id, out IApplicationView view); int GetViewInFocus(out IntPtr view); $(if ($OSBuild -ge 17134) {@" // Windows 10 1803 or up and Windows 11: int Unknown1(out IntPtr view); "@ }) void RefreshCollection(); int RegisterForApplicationViewChanges(object listener, out int cookie); $(if ($OSBuild -lt 17661) {@" // Windows 10 1607 and Server 2016 and Windows 10 1803: int RegisterForApplicationViewPositionChanges(object listener, out int cookie); "@ }) int UnregisterForApplicationViewChanges(int cookie); } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] $(if ($OSBuild -ge 22000) {@" // Windows 11: [Guid("536D3495-B208-4CC9-AE26-DE8111275BF8")] "@ }) $(if ($OSBuild -eq 20348) {@" // Windows Server 2022: [Guid("62fdf88b-11ca-4afb-8bd8-2296dfae49e2")] "@ }) $(if ($OSBuild -lt 20348) {@" // Windows 10: [Guid("FF72FFDD-BE7E-43FC-9C03-AD81681E88E4")] "@ }) internal interface IVirtualDesktop { bool IsViewVisible(IApplicationView view); Guid GetId(); $(if ($OSBuild -ge 20348) {@" // Windows Server 2022 and Windows 11: IntPtr Unknown1(); HString GetName(); "@ }) $(if ($OSBuild -ge 22000) {@" // Windows 11: HString GetWallpaperPath(); "@ }) } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] $(if ($OSBuild -ge 22000) {@" // Windows 11: [Guid("B2F925B9-5A0F-4D2E-9F4D-2B1507593C10")] internal interface IVirtualDesktopManagerInternal { int GetCount(IntPtr hWndOrMon); void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); bool CanViewMoveDesktops(IApplicationView view); IVirtualDesktop GetCurrentDesktop(IntPtr hWndOrMon); "@ }) $(if ($OSBuild -ge 22449) {@" // Windows 11 Insider: IObjectArray GetAllCurrentDesktops(); "@ }) $(if ($OSBuild -ge 22000) {@" void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops); [PreserveSig] int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop); IVirtualDesktop CreateDesktop(IntPtr hWndOrMon); void MoveDesktop(IVirtualDesktop desktop, IntPtr hWndOrMon, int nIndex); void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); IVirtualDesktop FindDesktop(ref Guid desktopid); void GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop desktop, out IObjectArray unknown1, out IObjectArray unknown2); void SetDesktopName(IVirtualDesktop desktop, HString name); void SetDesktopWallpaper(IVirtualDesktop desktop, HString path); void UpdateWallpaperPathForAllDesktops(HString path); void CopyDesktopState(IApplicationView pView0, IApplicationView pView1); int GetDesktopIsPerMonitor(); void SetDesktopIsPerMonitor(bool state); } "@ }) $(if ($OSBuild -eq 20348) {@" // Windows Server 2022: [Guid("094afe11-44f2-4ba0-976f-29a97e263ee0")] internal interface IVirtualDesktopManagerInternal { int GetCount(IntPtr hWndOrMon); void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); bool CanViewMoveDesktops(IApplicationView view); IVirtualDesktop GetCurrentDesktop(IntPtr hWndOrMon); void GetDesktops(IntPtr hWndOrMon, out IObjectArray desktops); [PreserveSig] int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); void SwitchDesktop(IntPtr hWndOrMon, IVirtualDesktop desktop); IVirtualDesktop CreateDesktop(IntPtr hWndOrMon); void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); IVirtualDesktop FindDesktop(ref Guid desktopid); void GetDesktopSwitchIncludeExcludeViews(IVirtualDesktop desktop, out IObjectArray unknown1, out IObjectArray unknown2); void SetDesktopName(IVirtualDesktop desktop, HString name); void CopyDesktopState(IApplicationView pView0, IApplicationView pView1); int GetDesktopIsPerMonitor(); } "@ }) $(if ($OSBuild -lt 20348) {@" // Windows 10: [Guid("F31574D6-B682-4CDC-BD56-1827860ABEC6")] internal interface IVirtualDesktopManagerInternal { int GetCount(); void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); bool CanViewMoveDesktops(IApplicationView view); IVirtualDesktop GetCurrentDesktop(); void GetDesktops(out IObjectArray desktops); [PreserveSig] int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); void SwitchDesktop(IVirtualDesktop desktop); IVirtualDesktop CreateDesktop(); void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); IVirtualDesktop FindDesktop(ref Guid desktopid); } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("0F3A72B0-4566-487E-9A33-4ED302F6D6CE")] internal interface IVirtualDesktopManagerInternal2 { int GetCount(); void MoveViewToDesktop(IApplicationView view, IVirtualDesktop desktop); bool CanViewMoveDesktops(IApplicationView view); IVirtualDesktop GetCurrentDesktop(); void GetDesktops(out IObjectArray desktops); [PreserveSig] int GetAdjacentDesktop(IVirtualDesktop from, int direction, out IVirtualDesktop desktop); void SwitchDesktop(IVirtualDesktop desktop); IVirtualDesktop CreateDesktop(); void RemoveDesktop(IVirtualDesktop desktop, IVirtualDesktop fallback); IVirtualDesktop FindDesktop(ref Guid desktopid); void Unknown1(IVirtualDesktop desktop, out IntPtr unknown1, out IntPtr unknown2); void SetName(IVirtualDesktop desktop, HString name); } "@ }) [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("A5CD92FF-29BE-454C-8D04-D82879FB3F1B")] internal interface IVirtualDesktopManager { bool IsWindowOnCurrentVirtualDesktop(IntPtr topLevelWindow); Guid GetWindowDesktopId(IntPtr topLevelWindow); void MoveWindowToDesktop(IntPtr topLevelWindow, ref Guid desktopId); } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("4CE81583-1E4C-4632-A621-07A53543148F")] internal interface IVirtualDesktopPinnedApps { bool IsAppIdPinned(string appId); void PinAppID(string appId); void UnpinAppID(string appId); bool IsViewPinned(IApplicationView applicationView); void PinView(IApplicationView applicationView); void UnpinView(IApplicationView applicationView); } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("92CA9DCD-5622-4BBA-A805-5E9F541BD8C9")] internal interface IObjectArray { void GetCount(out int count); void GetAt(int index, ref Guid iid, [MarshalAs(UnmanagedType.Interface)]out object obj); } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("6D5140C1-7436-11CE-8034-00AA006009FA")] internal interface IServiceProvider10 { [return: MarshalAs(UnmanagedType.IUnknown)] object QueryService(ref Guid service, ref Guid riid); } #endregion #region COM wrapper internal static class DesktopManager { static DesktopManager() { var shell = (IServiceProvider10)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_ImmersiveShell)); VirtualDesktopManagerInternal = (IVirtualDesktopManagerInternal)shell.QueryService(Guids.CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal).GUID); $(if ($OSBuild -lt 20348) {@" // Windows 10: try { VirtualDesktopManagerInternal2 = (IVirtualDesktopManagerInternal2)shell.QueryService(Guids.CLSID_VirtualDesktopManagerInternal, typeof(IVirtualDesktopManagerInternal2).GUID); } catch { VirtualDesktopManagerInternal2 = null; } "@ }) VirtualDesktopManager = (IVirtualDesktopManager)Activator.CreateInstance(Type.GetTypeFromCLSID(Guids.CLSID_VirtualDesktopManager)); ApplicationViewCollection = (IApplicationViewCollection)shell.QueryService(typeof(IApplicationViewCollection).GUID, typeof(IApplicationViewCollection).GUID); VirtualDesktopPinnedApps = (IVirtualDesktopPinnedApps)shell.QueryService(Guids.CLSID_VirtualDesktopPinnedApps, typeof(IVirtualDesktopPinnedApps).GUID); } internal static IVirtualDesktopManagerInternal VirtualDesktopManagerInternal; $(if ($OSBuild -lt 20348) {@" // Windows 10: internal static IVirtualDesktopManagerInternal2 VirtualDesktopManagerInternal2; "@ }) internal static IVirtualDesktopManager VirtualDesktopManager; internal static IApplicationViewCollection ApplicationViewCollection; internal static IVirtualDesktopPinnedApps VirtualDesktopPinnedApps; internal static IVirtualDesktop GetDesktop(int index) { // get desktop with index $(if ($OSBuild -lt 20348) {@" int count = VirtualDesktopManagerInternal.GetCount(); "@ } else {@" int count = VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); "@ }) if (index < 0 || index >= count) throw new ArgumentOutOfRangeException("index"); IObjectArray desktops; $(if ($OSBuild -lt 20348) {@" VirtualDesktopManagerInternal.GetDesktops(out desktops); "@ } else {@" VirtualDesktopManagerInternal.GetDesktops(IntPtr.Zero, out desktops); "@ }) object objdesktop; desktops.GetAt(index, typeof(IVirtualDesktop).GUID, out objdesktop); Marshal.ReleaseComObject(desktops); return (IVirtualDesktop)objdesktop; } internal static int GetDesktopIndex(IVirtualDesktop desktop) { // get index of desktop int index = -1; Guid IdSearch = desktop.GetId(); IObjectArray desktops; $(if ($OSBuild -lt 20348) {@" VirtualDesktopManagerInternal.GetDesktops(out desktops); "@ } else {@" VirtualDesktopManagerInternal.GetDesktops(IntPtr.Zero, out desktops); "@ }) object objdesktop; $(if ($OSBuild -lt 20348) {@" for (int i = 0; i < VirtualDesktopManagerInternal.GetCount(); i++) "@ } else {@" for (int i = 0; i < VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); i++) "@ }) { desktops.GetAt(i, typeof(IVirtualDesktop).GUID, out objdesktop); if (IdSearch.CompareTo(((IVirtualDesktop)objdesktop).GetId()) == 0) { index = i; break; } } Marshal.ReleaseComObject(desktops); return index; } internal static IApplicationView GetApplicationView(this IntPtr hWnd) { // get application view to window handle IApplicationView view; ApplicationViewCollection.GetViewForHwnd(hWnd, out view); return view; } internal static string GetAppId(IntPtr hWnd) { // get Application ID to window handle string appId; hWnd.GetApplicationView().GetAppUserModelId(out appId); return appId; } } #endregion #region public interface public class WindowInformation { // stores window informations public string Title { get; set; } public int Handle { get; set; } } public class Desktop { // open registry key [DllImport("advapi32.dll", CharSet=CharSet.Auto)] private static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult); // read registry value [DllImport("advapi32.dll", SetLastError=true)] private static extern uint RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref int lpType, IntPtr lpData, ref int lpcbData); // close registry key [DllImport("advapi32.dll", SetLastError=true)] private static extern int RegCloseKey(UIntPtr hKey); // get window handle of current console window (even if powershell started in cmd) [DllImport("Kernel32.dll")] public static extern IntPtr GetConsoleWindow(); // get process id to window handle [DllImport("user32.dll")] private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); // get handle of active window [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); private static UIntPtr HKEY_CURRENT_USER = new UIntPtr(0x80000001u); private const int KEY_READ = 0x20019; private static string GetRegistryString(string registryPath, string valName) { // reads string value out of user registry UIntPtr hKey = UIntPtr.Zero; IntPtr pResult = IntPtr.Zero; string Result = null; try { if (RegOpenKeyEx(HKEY_CURRENT_USER, registryPath, 0, KEY_READ, out hKey) == 0) { int size = 0; int type = 1; // REG_SZ uint retVal = RegQueryValueEx(hKey, valName, 0, ref type, IntPtr.Zero, ref size); if (size != 0) { pResult = Marshal.AllocHGlobal(size); retVal = RegQueryValueEx(hKey, valName, 0, ref type, pResult, ref size); if (retVal == 0) { Result = Marshal.PtrToStringAnsi(pResult); } } } } catch { } finally { if (hKey != UIntPtr.Zero) { RegCloseKey(hKey); } if (pResult != IntPtr.Zero) { Marshal.FreeHGlobal(pResult); } } return Result; } private IVirtualDesktop ivd; private Desktop(IVirtualDesktop desktop) { this.ivd = desktop; } public override int GetHashCode() { // get hash return ivd.GetHashCode(); } public override bool Equals(object obj) { // compare with object var desk = obj as Desktop; return desk != null && object.ReferenceEquals(this.ivd, desk.ivd); } public static int Count { // return the number of desktops $(if ($OSBuild -lt 20348) {@" get { return DesktopManager.VirtualDesktopManagerInternal.GetCount(); } "@ } else {@" get { return DesktopManager.VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); } "@ }) } public static Desktop Current { // returns current desktop $(if ($OSBuild -lt 20348) {@" get { return new Desktop(DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop()); } "@ } else {@" get { return new Desktop(DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop(IntPtr.Zero)); } "@ }) } public static Desktop FromIndex(int index) { // return desktop object from index (-> index = 0..Count-1) return new Desktop(DesktopManager.GetDesktop(index)); } public static Desktop FromWindow(IntPtr hWnd) { // return desktop object to desktop on which window <hWnd> is displayed if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); Guid id = DesktopManager.VirtualDesktopManager.GetWindowDesktopId(hWnd); return new Desktop(DesktopManager.VirtualDesktopManagerInternal.FindDesktop(ref id)); } public static int FromDesktop(Desktop desktop) { // return index of desktop object or -1 if not found return DesktopManager.GetDesktopIndex(desktop.ivd); } public static string DesktopNameFromDesktop(Desktop desktop) { // return name of desktop or "Desktop n" if it has no name Guid guid = desktop.ivd.GetId(); // read desktop name in registry string desktopName = null; try { desktopName = GetRegistryString("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + guid.ToString() + "}", "Name"); } catch { } // no name found, generate generic name if (string.IsNullOrEmpty(desktopName)) { // create name "Desktop n" (n = number starting with 1) desktopName = "Desktop " + (DesktopManager.GetDesktopIndex(desktop.ivd) + 1).ToString(); } return desktopName; } public static string DesktopNameFromIndex(int index) { // return name of desktop from index (-> index = 0..Count-1) or "Desktop n" if it has no name Guid guid = DesktopManager.GetDesktop(index).GetId(); // read desktop name in registry string desktopName = null; try { desktopName = GetRegistryString("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + guid.ToString() + "}", "Name"); } catch { } // no name found, generate generic name if (string.IsNullOrEmpty(desktopName)) { // create name "Desktop n" (n = number starting with 1) desktopName = "Desktop " + (index + 1).ToString(); } return desktopName; } public static bool HasDesktopNameFromIndex(int index) { // return true is desktop is named or false if it has no name Guid guid = DesktopManager.GetDesktop(index).GetId(); // read desktop name in registry string desktopName = null; try { desktopName = GetRegistryString("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops\\Desktops\\{" + guid.ToString() + "}", "Name"); } catch { } // name found? if (string.IsNullOrEmpty(desktopName)) return false; else return true; } $(if ($OSBuild -ge 22000) {@" public static string DesktopWallpaperFromIndex(int index) { // return name of desktop wallpaper from index (-> index = 0..Count-1) // get desktop name string desktopwppath = ""; try { desktopwppath = DesktopManager.GetDesktop(index).GetWallpaperPath(); } catch { } return desktopwppath; } "@ }) public static int SearchDesktop(string partialName) { // get index of desktop with partial name, return -1 if no desktop found int index = -1; $(if ($OSBuild -lt 20348) {@" for (int i = 0; i < DesktopManager.VirtualDesktopManagerInternal.GetCount(); i++) "@ } else {@" for (int i = 0; i < DesktopManager.VirtualDesktopManagerInternal.GetCount(IntPtr.Zero); i++) "@ }) { // loop through all virtual desktops and compare partial name to desktop name if (DesktopNameFromIndex(i).ToUpper().IndexOf(partialName.ToUpper()) >= 0) { index = i; break; } } return index; } public static Desktop Create() { // create a new desktop $(if ($OSBuild -lt 20348) {@" return new Desktop(DesktopManager.VirtualDesktopManagerInternal.CreateDesktop()); "@ } else {@" return new Desktop(DesktopManager.VirtualDesktopManagerInternal.CreateDesktop(IntPtr.Zero)); "@ }) } public void Remove(Desktop fallback = null) { // destroy desktop and switch to <fallback> IVirtualDesktop fallbackdesktop; if (fallback == null) { // if no fallback is given use desktop to the left except for desktop 0. Desktop dtToCheck = new Desktop(DesktopManager.GetDesktop(0)); if (this.Equals(dtToCheck)) { // desktop 0: set fallback to second desktop (= "right" desktop) DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 4, out fallbackdesktop); // 4 = RightDirection } else { // set fallback to "left" desktop DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 3, out fallbackdesktop); // 3 = LeftDirection } } else // set fallback desktop fallbackdesktop = fallback.ivd; DesktopManager.VirtualDesktopManagerInternal.RemoveDesktop(ivd, fallbackdesktop); } $(if ($OSBuild -lt 20348) {@" public void SetName(string Name) { // set name for desktop, empty string removes name if (DesktopManager.VirtualDesktopManagerInternal2 != null) { // only if interface to set name is present HString hstring = HString.FromString(Name); DesktopManager.VirtualDesktopManagerInternal2.SetName(this.ivd, hstring); hstring.Delete(); } } "@ }) $(if ($OSBuild -ge 20348) {@" public void SetName(string Name) { // set name for desktop, empty string removes name HString hstring = HString.FromString(Name); DesktopManager.VirtualDesktopManagerInternal.SetDesktopName(this.ivd, hstring); hstring.Delete(); } "@ }) $(if ($OSBuild -ge 22000) {@" public static void RemoveAll() { // remove all desktops but visible DesktopManager.VirtualDesktopManagerInternal.SetDesktopIsPerMonitor(true); } public void Move(int index) { // move current desktop to desktop in index (-> index = 0..Count-1) DesktopManager.VirtualDesktopManagerInternal.MoveDesktop(ivd, IntPtr.Zero, index); } public void SetWallpaperPath(string Path) { // set path for wallpaper, empty string removes path if (string.IsNullOrEmpty(Path)) throw new ArgumentNullException(); HString hstring = HString.FromString(Path); DesktopManager.VirtualDesktopManagerInternal.SetDesktopWallpaper(this.ivd, hstring); hstring.Delete(); } public static void SetAllWallpaperPaths(string Path) { // set wallpaper path for all desktops if (string.IsNullOrEmpty(Path)) throw new ArgumentNullException(); HString hstring = HString.FromString(Path); DesktopManager.VirtualDesktopManagerInternal.UpdateWallpaperPathForAllDesktops(hstring); hstring.Delete(); } "@ }) public bool IsVisible { // return true if this desktop is the current displayed one $(if ($OSBuild -lt 20348) {@" get { return object.ReferenceEquals(ivd, DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop()); } "@ } else {@" get { return object.ReferenceEquals(ivd, DesktopManager.VirtualDesktopManagerInternal.GetCurrentDesktop(IntPtr.Zero)); } "@ }) } public void MakeVisible() { // make this desktop visible $(if ($OSBuild -lt 20348) {@" DesktopManager.VirtualDesktopManagerInternal.SwitchDesktop(ivd); "@ } else {@" DesktopManager.VirtualDesktopManagerInternal.SwitchDesktop(IntPtr.Zero, ivd); "@ }) } public Desktop Left { // return desktop at the left of this one, null if none get { IVirtualDesktop desktop; int hr = DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 3, out desktop); // 3 = LeftDirection if (hr == 0) return new Desktop(desktop); else return null; } } public Desktop Right { // return desktop at the right of this one, null if none get { IVirtualDesktop desktop; int hr = DesktopManager.VirtualDesktopManagerInternal.GetAdjacentDesktop(ivd, 4, out desktop); // 4 = RightDirection if (hr == 0) return new Desktop(desktop); else return null; } } public void MoveWindow(IntPtr hWnd) { // move window to this desktop int processId; if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); GetWindowThreadProcessId(hWnd, out processId); if (hWnd == GetConsoleWindow()) { // own window try // the easy way (powershell's own console) { DesktopManager.VirtualDesktopManager.MoveWindowToDesktop(hWnd, ivd.GetId()); } catch // powershell in cmd console { IApplicationView view; DesktopManager.ApplicationViewCollection.GetViewForHwnd(hWnd, out view); DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd); } } else { // window of other process IApplicationView view; DesktopManager.ApplicationViewCollection.GetViewForHwnd(hWnd, out view); try { DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd); } catch { // could not move active window, try main window (or whatever windows thinks is the main window) DesktopManager.ApplicationViewCollection.GetViewForHwnd(System.Diagnostics.Process.GetProcessById(processId).MainWindowHandle, out view); DesktopManager.VirtualDesktopManagerInternal.MoveViewToDesktop(view, ivd); } } } public void MoveActiveWindow() { // move active window to this desktop MoveWindow(GetForegroundWindow()); } public bool HasWindow(IntPtr hWnd) { // return true if window is on this desktop if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); return ivd.GetId() == DesktopManager.VirtualDesktopManager.GetWindowDesktopId(hWnd); } public static bool IsWindowPinned(IntPtr hWnd) { // return true if window is pinned to all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); return DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(hWnd.GetApplicationView()); } public static void PinWindow(IntPtr hWnd) { // pin window to all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); var view = hWnd.GetApplicationView(); if (!DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(view)) { // pin only if not already pinned DesktopManager.VirtualDesktopPinnedApps.PinView(view); } } public static void UnpinWindow(IntPtr hWnd) { // unpin window from all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); var view = hWnd.GetApplicationView(); if (DesktopManager.VirtualDesktopPinnedApps.IsViewPinned(view)) { // unpin only if not already unpinned DesktopManager.VirtualDesktopPinnedApps.UnpinView(view); } } public static bool IsApplicationPinned(IntPtr hWnd) { // return true if application for window is pinned to all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); return DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(DesktopManager.GetAppId(hWnd)); } public static void PinApplication(IntPtr hWnd) { // pin application for window to all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); string appId = DesktopManager.GetAppId(hWnd); if (!DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(appId)) { // pin only if not already pinned DesktopManager.VirtualDesktopPinnedApps.PinAppID(appId); } } public static void UnpinApplication(IntPtr hWnd) { // unpin application for window from all desktops if (hWnd == IntPtr.Zero) throw new ArgumentNullException(); var view = hWnd.GetApplicationView(); string appId = DesktopManager.GetAppId(hWnd); if (DesktopManager.VirtualDesktopPinnedApps.IsAppIdPinned(appId)) { // unpin only if pinned DesktopManager.VirtualDesktopPinnedApps.UnpinAppID(appId); } } // prepare callback function for window enumeration private delegate bool CallBackPtr(int hwnd, int lParam); private static CallBackPtr callBackPtr = Callback; // list of window informations private static List<WindowInformation> WindowInformationList = new List<WindowInformation>(); // enumerate windows [DllImport("User32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam); // get window title length [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowTextLength(IntPtr hWnd); // get window title [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); // callback function for window enumeration private static bool Callback(int hWnd, int lparam) { int length = GetWindowTextLength((IntPtr)hWnd); if (length > 0) { StringBuilder sb = new StringBuilder(length + 1); if (GetWindowText((IntPtr)hWnd, sb, sb.Capacity) > 0) { WindowInformationList.Add(new WindowInformation {Handle = hWnd, Title = sb.ToString()}); } } return true; } // get list of all windows with title public static List<WindowInformation> GetWindows() { WindowInformationList = new List<WindowInformation>(); EnumWindows(callBackPtr, IntPtr.Zero); return WindowInformationList; } // find first window with string in title public static WindowInformation FindWindow(string WindowTitle) { WindowInformationList = new List<WindowInformation>(); EnumWindows(callBackPtr, IntPtr.Zero); WindowInformation result = WindowInformationList.Find(x => x.Title.IndexOf(WindowTitle, StringComparison.OrdinalIgnoreCase) >= 0); return result; } } #endregion } "@ function Get-DesktopCount { <# .SYNOPSIS Get count of virtual desktops .DESCRIPTION Get count of virtual desktops .INPUTS None .OUTPUTS Int32 .EXAMPLE Get-DesktopCount Get count of virtual desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param() Write-Verbose "Count of virtual desktops: $([VirtualDesktop.Desktop]::Count)" return [VirtualDesktop.Desktop]::Count } if ($OSBuild -ge 22000) { function Get-DesktopList { <# .SYNOPSIS Get list of virtual desktops .DESCRIPTION Get list of virtual desktops .INPUTS None .OUTPUTS Object .EXAMPLE Get-DesktopList Get list of virtual desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2020/06/27 Updated: 2021/10/17 #> $DesktopList = @() for ($I = 0; $I -lt [VirtualDesktop.Desktop]::Count; $I++) { $DesktopList += [PSCustomObject]@{ Number = $I Name = [VirtualDesktop.Desktop]::DesktopNameFromIndex($I) Wallpaper = [VirtualDesktop.Desktop]::DesktopWallpaperFromIndex($I) Visible = if ([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current) -eq $I) { $TRUE } else { $FALSE } } } return $DesktopList } } else { function Get-DesktopList { <# .SYNOPSIS Get list of virtual desktops .DESCRIPTION Get list of virtual desktops .INPUTS None .OUTPUTS Object .EXAMPLE Get-DesktopList Get list of virtual desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2020/06/27 #> $DesktopList = @() for ($I = 0; $I -lt [VirtualDesktop.Desktop]::Count; $I++) { $DesktopList += [PSCustomObject]@{ Number = $I Name = [VirtualDesktop.Desktop]::DesktopNameFromIndex($I) Visible = if ([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current) -eq $I) { $TRUE } else { $FALSE } } } return $DesktopList } } function New-Desktop { <# .SYNOPSIS Create virtual desktop .DESCRIPTION Create virtual desktop .INPUTS None .OUTPUTS Desktop object .EXAMPLE New-Desktop | Switch-Desktop Create virtual desktop and switch to it .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param() $Desktop = [VirtualDesktop.Desktop]::Create() Write-Verbose "Created desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop))" return $Desktop } function Switch-Desktop { <# .SYNOPSIS Switch to virtual desktop .DESCRIPTION Switch to virtual desktop .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS None .EXAMPLE Switch-Desktop 0 Switch to first virtual desktop .EXAMPLE Switch-Desktop $Desktop Switch to virtual desktop $Desktop .EXAMPLE "Desktop 1" | Switch-Desktop Switch to second virtual desktop .EXAMPLE New-Desktop | Switch-Desktop Create virtual desktop and switch to it .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($Desktop -is [VirtualDesktop.Desktop]) { $Desktop.MakeVisible() Write-Verbose "Switched to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { $TempDesktop.MakeVisible() Write-Verbose "Switched to desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { ([VirtualDesktop.Desktop]::FromIndex($TempIndex)).MakeVisible() Write-Verbose "Switched to desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } } } function Remove-Desktop { <# .SYNOPSIS Remove virtual desktop .DESCRIPTION Remove virtual desktop. Windows on the desktop to be removed are moved to the virtual desktop to the left except for desktop 0 where the second desktop is used instead. If the current desktop is removed, this fallback desktop is activated too. If no desktop is supplied, the last desktop is removed. .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS None .EXAMPLE Remove-Desktop 0 Remove first virtual desktop .EXAMPLE Remove-Desktop $Desktop Remove virtual desktop $Desktop .EXAMPLE "Desktop 1" | Remove-Desktop Remove second virtual desktop .EXAMPLE New-Desktop | Remove-Desktop Create virtual desktop and remove it immediately .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::FromIndex(([VirtualDesktop.Desktop]::Count) -1) } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Removing desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" $Desktop.Remove() } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Removing desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" $TempDesktop.Remove() } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Removing desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" ([VirtualDesktop.Desktop]::FromIndex($TempIndex)).Remove() } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } } } if ($OSBuild -ge 22000) { function Remove-AllDesktops { <# .SYNOPSIS Remove all virtual desktops but visible .DESCRIPTION Remove all virtual desktops but visible .INPUTS None .OUTPUTS None .EXAMPLE Remove-AllDesktops Remove all virtual desktops but visible .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .NOTES Author: Markus Scholtes Created: 2021/10/17 #> [VirtualDesktop.Desktop]::RemoveAll() } } function Get-CurrentDesktop { <# .SYNOPSIS Get current virtual desktop .DESCRIPTION Get current virtual desktop as Desktop object .INPUTS None .OUTPUTS Desktop object .EXAMPLE Get-CurrentDesktop | Remove-Desktop Remove current virtual desktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param() $Desktop = [VirtualDesktop.Desktop]::Current Write-Verbose "Current desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop } function Get-Desktop { <# .SYNOPSIS Get virtual desktop with index number (0 to count-1) or string (part of desktop name) .DESCRIPTION Get virtual desktop with index number (0 to count-1) or string (part of desktop name) Returns $NULL if index number is out of range. Returns current desktop is index is omitted. .PARAMETER Index Number of desktop (starting with 0 to count-1) or string (part of desktop name) .INPUTS Int32 or STRING .OUTPUTS Desktop object .EXAMPLE Get-Desktop 1 | Switch-Desktop Get object of second virtual desktop and switch to it .EXAMPLE "Desktop 1" | Get-Desktop | Switch-Desktop Get object of second virtual desktop and switch to it .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([VirtualDesktop.Desktop])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Index) if ($NULL -eq $Index) { $Desktop = [VirtualDesktop.Desktop]::Current Write-Verbose "Current desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop } if ($Index -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Index) if ($NULL -ne $TempDesktop) { Write-Verbose "Desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" } return $TempDesktop } else { if ($Index -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Index) if ($TempIndex -ge 0) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($TempIndex) Write-Verbose "Desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" return $TempDesktop } else { Write-Error "No desktop with name part '$Index' found" return $NULL } } else { Write-Error "Parameter -Index has to be an integer or string" return $NULL } } } function Get-DesktopIndex { <# .SYNOPSIS Get index number (0 to count-1) of virtual desktop .DESCRIPTION Get index number (0 to count-1) of virtual desktop Returns -1 if desktop cannot be found. Returns index of current desktop is parameter desktop is omitted. .PARAMETER Desktop Desktop object or string (part of desktop name) .INPUTS Desktop object or string (part of desktop name) .OUTPUTS Int32 .EXAMPLE New-Desktop | Get-DesktopIndex Get index number of new virtual desktop .EXAMPLE Get-DesktopIndex "desktop 1" Get index number of desktop with name containing "desktop 1" .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2021/02/28 #> [OutputType([INT32])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return [VirtualDesktop.Desktop]::FromDesktop($Desktop) } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Desktop number $TempIndex ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" return $TempIndex } else { Write-Error "No desktop with name part '$Desktop' found" return -1 } } else { Write-Error "Parameter -Desktop has to be a desktop object or string" return -1 } } } function Get-DesktopName { <# .SYNOPSIS Get name of virtual desktop .DESCRIPTION Get name of virtual desktop .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS String (name of desktop) .EXAMPLE Get-DesktopName 0 Get name of first desktop .EXAMPLE Get-DesktopName $Desktop Get name of virtual desktop $Desktop .EXAMPLE "desktop" | Get-DesktopName Get name of first virtual desktop whose name contains "desktop" .EXAMPLE New-Desktop | Get-DesktopName Create virtual desktop and show its name .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2020/06/27 Updated: 2021/02/28 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Get name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return ([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop)) } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Get name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" return ([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop)) } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Get name of desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" return ([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex))) } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } } } function Set-DesktopName { <# .SYNOPSIS Set name of virtual desktop .DESCRIPTION Set name of virtual desktop. If parameter Desktop is not set, the name of the current desktop is used. .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .PARAMETER Name Name of desktop. If omitted or empty or $NULL, a name will be removed from the desktop. .PARAMETER PassThru Return virtual desktop .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS None or [VirtualDesktop.Desktop] .EXAMPLE Set-DesktopName 0 "The first desktop" Set name of first desktop .EXAMPLE Set-DesktopName $Desktop Remove name of virtual desktop $Desktop .EXAMPLE "desktop" | Set-DesktopName -Name "First found" Set name of first virtual desktop whose name contains "desktop" .EXAMPLE Set-DesktopName -Name "This is the current desktop" Set name of the current virtual desktop .EXAMPLE New-Desktop | Set-DesktopName -Name "The new one" -PassThru | Get-DesktopName Create virtual desktop, set its name and return the new name .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2020/06/27 Updated: 2021/10/18 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop, [Parameter(ValueFromPipeline = $FALSE)] $Name, [SWITCH]$PassThru) if ($NULL -eq $Name) { $Name = "" } if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { if ($Name -ne "") { Write-Verbose "Set name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))') to '$Name'" } else { Write-Verbose "Remove name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" } $Desktop.SetName($Name) $ActiveDesktop = $Desktop } else { if ($Desktop -is [ValueType]) { $ActiveDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($ActiveDesktop) { if ($Name -ne "") { Write-Verbose "Set name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))') to '$Name'" } else { Write-Verbose "Remove name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))')" } $ActiveDesktop.SetName($Name) } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { $ActiveDesktop = [VirtualDesktop.Desktop]::FromIndex($TempIndex) if ($Name -ne "") { Write-Verbose "Set name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))') to '$Name'" } else { Write-Verbose "Remove name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))')" } $ActiveDesktop.SetName($Name) } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } } if ($PassThru) { return $ActiveDesktop } } if ($OSBuild -ge 22000) { function Set-DesktopWallpaper { <# .SYNOPSIS Set wallpaper of virtual desktop .DESCRIPTION Set wallpaper of virtual desktop .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) If parameter Desktop is not set, the name of the current desktop is used. .PARAMETER Path Path to wallpaper .PARAMETER PassThru Return virtual desktop .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS None or [VirtualDesktop.Desktop] .EXAMPLE Set-DesktopWallpaper 0 "C:\Users\VD\Pictures\NicePic.jpg" Set wallpaper of first desktop .EXAMPLE Set-DesktopWallpaper -Path "C:\Users\VD\Pictures\CurrentDesktopPic.jpg" Set wallpaper of current desktop .EXAMPLE "First found" | Set-DesktopWallpaper -Path "C:\Windows\Web\Wallpaper\Windows\img0.jpg" Set wallpaper of first virtual desktop whose name contains "First found" .EXAMPLE New-Desktop | Set-DesktopWallpaper -Path "Background.jpg" -PassThru | Get-DesktopName Create virtual desktop, set its wallpaper and return the name .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .NOTES Author: Markus Scholtes Created: 2021/10/18 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop, [Parameter(ValueFromPipeline = $FALSE)] $Path, [SWITCH]$PassThru) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { if ([STRING]::IsNullOrEmpty($Path)) { Write-Error "Wallpaper path is missing" } else { Write-Verbose "Set wallpaper of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))') to '$Path'" $Desktop.SetWallpaperPath($Path) } $ActiveDesktop = $Desktop } else { if ($Desktop -is [ValueType]) { $ActiveDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($ActiveDesktop) { if ([STRING]::IsNullOrEmpty($Path)) { Write-Error "Wallpaper path is missing" } else { Write-Verbose "Set name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))') to '$Path'" $ActiveDesktop.SetWallpaperPath($Path) } } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { $ActiveDesktop = [VirtualDesktop.Desktop]::FromIndex($TempIndex) if ([STRING]::IsNullOrEmpty($Path)) { Write-Error "Wallpaper path is missing" } else { Write-Verbose "Set name of desktop number $([VirtualDesktop.Desktop]::FromDesktop($ActiveDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($ActiveDesktop))') to '$Path'" $ActiveDesktop.SetWallpaperPath($Path) } } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } } if ($PassThru) { return $ActiveDesktop } } function Set-AllDesktopWallpapers { <# .SYNOPSIS Set wallpaper of all virtual desktops .DESCRIPTION Set wallpaper of all virtual desktops .PARAMETER Path Path to wallpaper .INPUTS String .OUTPUTS None .EXAMPLE Set-AllDesktopWallpapers -Path "C:\Users\VD\Pictures\NicePic.jpg" Set wallpaper of all desktops .EXAMPLE "C:\Windows\Web\Wallpaper\Windows\img0.jpg" | Set-AllDesktopWallpapers Set wallpaper of all desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .NOTES Author: Markus Scholtes Created: 2021/10/17 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Path) if ([STRING]::IsNullOrEmpty($Path)) { Write-Error "Wallpaper path is missing" } else { Write-Verbose "Set wallpaper of all desktops to '$Path'" [VirtualDesktop.Desktop]::SetAllWallpaperPaths($Path) } } } function Get-DesktopFromWindow { <# .SYNOPSIS Get virtual desktop of window .DESCRIPTION Get virtual desktop of window whose window handle is given. Returns $NULL if window handle is unknown. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS Desktop object .EXAMPLE Get-DesktopFromWindow ((Get-Process "notepad")[0].MainWindowHandle) | Switch-Desktop Switch to virtual desktop with notepad window .EXAMPLE Find-WindowHandle "notepad" | Get-DesktopFromWindow | Switch-Desktop Switch to virtual desktop with notepad window .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([VirtualDesktop.Desktop])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { $Desktop = [VirtualDesktop.Desktop]::FromWindow($Hwnd) if ($NULL -ne $Desktop) { Write-Verbose "Window is on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" } return $Desktop } else { if ($Hwnd -is [ValueType]) { $Desktop = [VirtualDesktop.Desktop]::FromWindow([IntPtr]$Hwnd) if ($NULL -ne $Desktop) { Write-Verbose "Window is on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" } return $Desktop } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" return $NULL } } } function Test-CurrentDesktop { <# .SYNOPSIS Checks whether a desktop is the displayed virtual desktop .DESCRIPTION Checks whether a desktop is the displayed virtual desktop .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS Boolean .EXAMPLE Get-DesktopIndex 1 | Test-CurrentDesktop Checks whether the desktop with count number 1 is the displayed virtual desktop .EXAMPLE Test-CurrentDesktop "desktop 2" Checks whether the desktop with string "desktop 2" in name is the displayed virtual desktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([BOOLEAN])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Check visibility of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop.IsVisible } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Check visibility of desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" return $TempDesktop.IsVisible } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Check visibility of desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" return ([VirtualDesktop.Desktop]::FromIndex($TempIndex)).IsVisible } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } return $FALSE } } function Get-LeftDesktop { <# .SYNOPSIS Get the desktop object on the "left" side .DESCRIPTION Get the desktop object on the "left" side If there is no desktop on the "left" side $NULL is returned. Returns desktop "left" to current desktop if parameter desktop is omitted. .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS Desktop object .EXAMPLE Get-CurrentDesktop | Get-LeftDesktop | Switch-Desktop Switch to the desktop left of the displayed virtual desktop .EXAMPLE Get-LeftDesktop 1 Get desktop left to second desktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Returning desktop left of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop.Left } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Returning desktop left of desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" return $TempDesktop.Left } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Returning desktop left of desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" return ([VirtualDesktop.Desktop]::FromIndex($TempIndex)).Left } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } return $NULL } } function Get-RightDesktop { <# .SYNOPSIS Get the desktop object on the "right" side .DESCRIPTION Get the desktop object on the "right" side If there is no desktop on the "right" side $NULL is returned. Returns desktop "right" to current desktop if parameter desktop is omitted. .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS Desktop object .EXAMPLE Get-CurrentDesktop | Get-RightDesktop | Switch-Desktop Switch to the desktop right of the displayed virtual desktop .EXAMPLE Get-RightDesktop 1 Get desktop right to second desktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Returning desktop right of desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop.Right } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Returning desktop right of desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" return $TempDesktop.Right } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { Write-Verbose "Returning desktop right of desktop number $([VirtualDesktop.Desktop]::FromDesktop(([VirtualDesktop.Desktop]::FromIndex($TempIndex)))) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::FromIndex($TempIndex)))')" return ([VirtualDesktop.Desktop]::FromIndex($TempIndex)).Right } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } return $NULL } } if ($OSBuild -ge 22000) { function Move-Desktop { <# .SYNOPSIS Move current desktop to other virtual desktop .DESCRIPTION Move current desktop to other virtual desktop. .PARAMETER Desktop Desktop object to move current desktop to .INPUTS None .OUTPUTS Desktop object .EXAMPLE Move-Window -Desktop (Get-Desktop "Other Desktop") Move current virtual desktop to desktop "Other Desktop" .EXAMPLE Move-Window -Desktop (Get-RightDesktop) Move current virtual desktop to the "right" .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .NOTES Author: Markus Scholtes Created: 2021/10/17 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { Write-Error "Parameter -Desktop missing" return $NULL } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Moving current desktop to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" ([VirtualDesktop.Desktop]::Current).Move([VirtualDesktop.Desktop]::FromDesktop($Desktop)) return ([VirtualDesktop.Desktop]::Current) } Write-Error "Parameter -Desktop has to be a desktop object" return $NULL } } function Move-Window { <# .SYNOPSIS Move window to virtual desktop .DESCRIPTION Move window whose window handle is given to virtual desktop. The parameter values are auto detected and can change places. The desktop object is handed to the output pipeline for further use. If parameter desktop is omitted, the current desktop is used. .PARAMETER Desktop Desktop object .PARAMETER Hwnd Window handle .INPUTS Desktop object .OUTPUTS Desktop object .EXAMPLE Move-Window -Desktop (Get-CurrentDesktop) -Hwnd ((Get-Process "notepad")[0].MainWindowHandle) Move notepad window to current virtual desktop .EXAMPLE New-Desktop | Move-Window (Get-ConsoleHandle) | Switch-Desktop Create virtual desktop and move powershell console window to it, then activate new desktop. .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $FALSE)] $Desktop, [Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } else { if ($NULL -eq $Hwnd) { $Hwnd = [VirtualDesktop.Desktop]::Current } } if (($Hwnd -is [IntPtr]) -And ($Desktop -is [VirtualDesktop.Desktop])) { Write-Verbose "Moving window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" $Desktop.MoveWindow($Hwnd) return $Desktop } if (($Hwnd -is [ValueType]) -And ($Desktop -is [VirtualDesktop.Desktop])) { Write-Verbose "Moving window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" $Desktop.MoveWindow([IntPtr]$Hwnd) return $Desktop } if (($Desktop -is [IntPtr]) -And ($Hwnd -is [VirtualDesktop.Desktop])) { Write-Verbose "Moving window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Hwnd)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Hwnd))')" $Hwnd.MoveWindow($Desktop) return $Hwnd } if (($Desktop -is [ValueType]) -And ($Hwnd -is [VirtualDesktop.Desktop])) { Write-Verbose "Moving window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Hwnd)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Hwnd))')" $Hwnd.MoveWindow([IntPtr]$Desktop) return $Hwnd } Write-Error "Parameters -Desktop and -Hwnd have to be a desktop object and an IntPtr/integer pair" return $NULL } function Move-ActiveWindow { <# .SYNOPSIS Move active window to virtual desktop .DESCRIPTION Move active window to virtual desktop. The desktop object is handed to the output pipeline for further use. If parameter desktop is omitted, the current desktop is used. .PARAMETER Desktop Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .INPUTS Number of desktop (starting with 0 to count-1), desktop object or string (part of desktop name) .OUTPUTS Desktop object .EXAMPLE Move-ActiveWindow -Desktop (Get-CurrentDesktop) Move active window to current virtual desktop .EXAMPLE New-Desktop | Move-ActiveWindow | Switch-Desktop Create virtual desktop and move activate window to it, then activate new desktop. .EXAMPLE Move-ActiveWindow "Desktop 2" Move activate window to second desktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2019/02/13 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Desktop) if ($NULL -eq $Desktop) { $Desktop = [VirtualDesktop.Desktop]::Current } if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Moving active window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" $Desktop.MoveWindow((Get-ActiveWindowHandle)) return $Desktop } else { if ($Desktop -is [ValueType]) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($Desktop) if ($TempDesktop) { Write-Verbose "Moving active window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" $TempDesktop.MoveWindow((Get-ActiveWindowHandle)) return $TempDesktop } } else { if ($Desktop -is [STRING]) { $TempIndex = [VirtualDesktop.Desktop]::SearchDesktop($Desktop) if ($TempIndex -ge 0) { $TempDesktop = [VirtualDesktop.Desktop]::FromIndex($TempIndex) Write-Verbose "Moving active window to desktop number $([VirtualDesktop.Desktop]::FromDesktop($TempDesktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($TempDesktop))')" $TempDesktop.MoveWindow((Get-ActiveWindowHandle)) return $TempDesktop } else { Write-Error "No desktop with name part '$Desktop' found" } } else { Write-Error "Parameter -Desktop has to be a desktop object, an integer or a string" } } return $NULL } } function Test-Window { <# .SYNOPSIS Check if window is displayed on virtual desktop .DESCRIPTION Check if window whose window handle is given is displayed on virtual desktop. The parameter values are auto detected and can change places. If parameter desktop is not supplied, the current desktop is used. .PARAMETER Desktop Desktop object. If omitted the current desktop is used. .PARAMETER Hwnd Window handle .INPUTS Desktop object .OUTPUTS Boolean .EXAMPLE Test-Window -Hwnd ((Get-Process "notepad")[0].MainWindowHandle) Check if notepad window is displayed on current virtual desktop .EXAMPLE Get-Desktop 1 | Test-Window (Get-ConsoleHandle) Check if powershell console window is displayed on virtual desktop with number 1 (second desktop) .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([BOOLEAN])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $FALSE)] $Desktop, [Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop.HasWindow($Hwnd) } else { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::Current))')" return ([VirtualDesktop.Desktop]::Current).HasWindow($Hwnd) } } if ($Hwnd -is [ValueType]) { if ($Desktop -is [VirtualDesktop.Desktop]) { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Desktop)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Desktop))')" return $Desktop.HasWindow([IntPtr]$Hwnd) } else { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::Current))')" return ([VirtualDesktop.Desktop]::Current).HasWindow([IntPtr]$Hwnd) } } if ($Desktop -is [IntPtr]) { if ($Hwnd -is [VirtualDesktop.Desktop]) { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Hwnd)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Hwnd))')" return $Hwnd.HasWindow($Desktop) } else { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::Current))')" return ([VirtualDesktop.Desktop]::Current).HasWindow($Desktop) } } if ($Desktop -is [ValueType]) { if ($Hwnd -is [VirtualDesktop.Desktop]) { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop($Hwnd)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop($Hwnd))')" return $Hwnd.HasWindow([IntPtr]$Desktop) } else { Write-Verbose "Checking window on desktop number $([VirtualDesktop.Desktop]::FromDesktop([VirtualDesktop.Desktop]::Current)) ('$([VirtualDesktop.Desktop]::DesktopNameFromDesktop([VirtualDesktop.Desktop]::Current))')" return ([VirtualDesktop.Desktop]::Current).HasWindow([IntPtr]$Desktop) } } Write-Error "Parameters -Desktop and -Hwnd have to be a desktop object and an IntPtr/integer pair" return $FALSE } function Pin-Window { <# .SYNOPSIS Pin window to all desktops .DESCRIPTION Pin window whose window handle is given to all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS None .EXAMPLE Pin-Window ((Get-Process "notepad")[0].MainWindowHandle) Pin notepad window to all desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { [VirtualDesktop.Desktop]::PinWindow($Hwnd) Write-Verbose "Pinned window with handle $Hwnd to all desktops" } else { if ($Hwnd -is [ValueType]) { [VirtualDesktop.Desktop]::PinWindow([IntPtr]$Hwnd) Write-Verbose "Pinned window with handle $Hwnd to all desktops" } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" } } } function Unpin-Window { <# .SYNOPSIS Unpin window from all desktops .DESCRIPTION Unpin window whose window handle is given from all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS None .EXAMPLE Unpin-Window ((Get-Process "notepad")[0].MainWindowHandle) Unpin notepad window from all desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { [VirtualDesktop.Desktop]::UnpinWindow($Hwnd) Write-Verbose "Unpinned window with handle $Hwnd from all desktops" } else { if ($Hwnd -is [ValueType]) { [VirtualDesktop.Desktop]::UnpinWindow([IntPtr]$Hwnd) Write-Verbose "Unpinned window with handle $Hwnd from all desktops" } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" } } } function Test-WindowPinned { <# .SYNOPSIS Checks whether a window is pinned to all desktops .DESCRIPTION Checks whether a window whose window handle is given is pinned to all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS Boolean .EXAMPLE Test-WindowPinned ((Get-Process "notepad")[0].MainWindowHandle) Checks whether notepad window is pinned to all virtual desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([BOOLEAN])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { Write-Verbose "Check if window with handle $Hwnd is pinned to all desktops" return [VirtualDesktop.Desktop]::IsWindowPinned($Hwnd) } else { if ($Hwnd -is [ValueType]) { Write-Verbose "Check if window with handle $Hwnd is pinned to all desktops" return [VirtualDesktop.Desktop]::IsWindowPinned([IntPtr]$Hwnd) } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" return $FALSE } } } function Pin-Application { <# .SYNOPSIS Pin application to all desktops .DESCRIPTION Pin application whose window handle is given to all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS None .EXAMPLE Pin-Application ((Get-Process "notepad")[0].MainWindowHandle) Pin all notepad windows to all desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { [VirtualDesktop.Desktop]::PinApplication($Hwnd) Write-Verbose "Pinned application with window handle $Hwnd to all desktops" } else { if ($Hwnd -is [ValueType]) { [VirtualDesktop.Desktop]::PinApplication([IntPtr]$Hwnd) Write-Verbose "Pinned application with window handle $Hwnd to all desktops" } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" } } } function Unpin-Application { <# .SYNOPSIS Unpin application from all desktops .DESCRIPTION Unpin application whose window handle is given from all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS None .EXAMPLE Unpin-Application ((Get-Process "notepad")[0].MainWindowHandle) Unpin all notepad windows from all desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { [VirtualDesktop.Desktop]::UnpinApplication($Hwnd) Write-Verbose "Unpinned application with window handle $Hwnd from all desktops" } else { if ($Hwnd -is [ValueType]) { [VirtualDesktop.Desktop]::UnpinApplication([IntPtr]$Hwnd) Write-Verbose "Unpinned application with window handle $Hwnd from all desktops" } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" } } } function Test-ApplicationPinned { <# .SYNOPSIS Checks whether an application is pinned to all desktops .DESCRIPTION Checks whether an application whose window handle is given is pinned to all desktops. .PARAMETER Hwnd Window handle .INPUTS IntPtr .OUTPUTS Boolean .EXAMPLE Test-ApplicationPinned ((Get-Process "notepad")[0].MainWindowHandle) Checks whether notepad windows are pinned to all virtual desktops .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2017/05/08 Updated: 2020/06/27 #> [OutputType([BOOLEAN])] [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Hwnd) if ($Hwnd -is [IntPtr]) { Write-Verbose "Check if application with window handle $Hwnd is pinned to all desktops" return [VirtualDesktop.Desktop]::IsApplicationPinned($Hwnd) } else { if ($Hwnd -is [ValueType]) { Write-Verbose "Check if application with window handle $Hwnd is pinned to all desktops" return [VirtualDesktop.Desktop]::IsApplicationPinned([IntPtr]$Hwnd) } else { Write-Error "Parameter -Hwnd has to be an IntPtr or an integer" return $FALSE } } } function Get-ConsoleHandle { <# .SYNOPSIS Get window handle of powershell console .DESCRIPTION Get window handle of powershell console in a safe way (means: if powershell is started in a cmd window, the cmd window handled is returned). .INPUTS None .OUTPUTS IntPtr .EXAMPLE Get-ConsoleHandle Get window handle of powershell console .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2018/10/22 Updated: 2022/02/25 #> [Cmdletbinding()] Param() Write-Verbose "Retrieving console window handle" if ($NULL -ne $ENV:wt_session) { # seems to be running in Windows Terminal $HANDLE = (Get-Process -PID ((Get-WmiObject -Class win32_process -Filter "processid='$PID'").ParentProcessId)).MainWindowHandle } else { # Powershell in own console $HANDLE = [VirtualDesktop.Desktop]::GetConsoleWindow() if ($HANDLE -eq 0) { # maybe script is started in ISE $HANDLE = (Get-Process -PID $PID).MainWindowHandle } } return $HANDLE } function Get-ActiveWindowHandle { <# .SYNOPSIS Get window handle of foreground window .DESCRIPTION Get window handle of foreground window (the foreground window is always on the current virtual desktop). .INPUTS None .OUTPUTS IntPtr .EXAMPLE Get-ActiveWindowHandle Get window handle of foreground window .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2019/02/13 Updated: 2020/06/27 #> [Cmdletbinding()] Param() Write-Verbose "Retrieving handle of active window" return [VirtualDesktop.Desktop]::GetForegroundWindow() } function Find-WindowHandle { <# .SYNOPSIS Find window handle to title text or retrieve list of windows with title .DESCRIPTION Find first window handle to partial title text (not case sensitive) or retrieve list of windows with title if * is supplied as title .PARAMETER Title Partial window title or *. The search is not case sensitive. .INPUTS STRING .OUTPUTS Int or Array of WindowInformation .EXAMPLE Find-WindowHandle powershell Get window handle of first powershell window .EXAMPLE Find-WindowHandle * Get a list of all windows with title .EXAMPLE Find-WindowHandle * | ? { $_.Title -match "firefox" } Find all windows that contain the text "firefox" in their title .LINK https://github.com/MScholtes/PSVirtualDesktop .LINK https://github.com/MScholtes/TechNet-Gallery/tree/master/VirtualDesktop .LINK https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5 .NOTES Author: Markus Scholtes Created: 2019/09/04 Updated: 2020/06/27 #> [Cmdletbinding()] Param([Parameter(ValueFromPipeline = $TRUE)] $Title) if ($Title -eq "*") { Write-Verbose "Retrieving window titles and handles of all windows with titles" return [VirtualDesktop.Desktop]::GetWindows() } else { Write-Verbose "Retrieving window handles of first window with '$Title' in title" $RESULT = [VirtualDesktop.Desktop]::FindWindow($Title) if ($RESULT) { Write-Verbose "Window '$($RESULT.Title)' found" return $RESULT.Handle } else { Write-Verbose "No window found" return 0 } } } # Clean up variables Remove-Variable -Name OSVer,OSBuild |