Comments

Latest Posts

Windows PowerShell Script / CMD Commands Collection

Starting to collect some PowerShell scripts for references. There are lots of them can be found from the references section.




Access Network Drive in PowerShell

I'm running PowerShell in a Windows 7 x64 virtual machine. I have a shared folder on the host mapped as a network drive (Z:). When I run PS normally I can access that drive just fine, but if I run it "as administrator" it tells me:

Set-Location : Cannot find drive. A drive with the name 'Z' does not exist.
At line:1 char:13
+ Set-Location <<<<  Z:
    + CategoryInfo          : ObjectNotFound: (Z:String) [Set-Location], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

In the end the fix was simply to re-map the drive letter while running as Administrator:

net use Z: "\\vmware-host\Shared Folders"

It doesn't have to be done from the same PowerShell instance (or from PowerShell at all) - it's just something that needs to be done once for the entire logon session.



Export a Computer List from Active Directory

Step 1: Run Powershell ISE

Open the Powershell ISE → Run the following script, adjusting the path for the export:

Step 2: Source Code (One Line)

Get-ADComputer -Filter * -Property * | Select-Object Name,OperatingSystem,OperatingSystemVersion,ipv4Address | Export-CSV ADcomputerslist.csv -NoTypeInformation -Encoding UTF8


Exported with more information, email address, name, SamAccountName, title, department, last logon data

Get-Aduser -Filter * -Properties *|select  EmailAddress,name,SamAccountName,GivenName,SurName,@{n='Manager';e={(Get-aduser $_.manager).name}},Title,Enabled,Description,Department,LastLogonDate|export-csv "C:\temp\all_users.csv"



List disabled users

For example, let’s display the list of disabled user accounts in domain:

Search-ADAccount -UsersOnly –AccountDisabled

You can limit the search scope to a specific Active Directory container (OU):

Search-ADAccount -UsersOnly –AccountDisabled –searchbase "OU=Admins,OU=Accounts,DC=woshub,DC=com"


If you need to get the list of the disabled users containing certain user attributes and present it as a graphic table to be sorted, run the following:

Search-ADAccount -UsersOnly AccountDisabled |sort LastLogonDate | Select Name,LastLogonDate,DistinguishedName |out-gridview -title "Disabled Users"



Reset Domain User's Password

1. Using Powershell

Set-ADAccountPassword -Identity admin2 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity admin3 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity auditor1 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity auditor2 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity test1 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity test2 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity test3 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity vaultadmin1 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)
Set-ADAccountPassword -Identity vaultadmin2 -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "Cyberark1!" -Force)

2. Use Command Prompt

net user /domain USERNAME NEWPASS.
Run command prompt as administrator:
C:\Windows\system32>net user /domain admin2 Cyberark1!
The command completed successfully.
Replace USERNAME and NEWPASS with the actual username and a new password for this user.If the actual username consists of more than two words, place it inside quotation marks.




For Exchange Online Management 



PS C:\Windows\system32> Install-Module -Name ExchangeOnlineManagement -RequiredVersion 2.0.5                                                                                                                                                    NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet
 provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\xxyan\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and
import the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

PS C:\Windows\system32> Set-ExecutionPolicy RemoteSigned

Execution Policy Change
The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
you to the security risks described in the about_Execution_Policies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS C:\Windows\system32>






Common CMD Commands



0. help(帮助)不懂得地方就可以用这个,对于初学者很有用,相当于一个母目录
1. gpedit.msc-----组策略
2. sndrec32-------录音机
3. Nslookup-------IP地址侦测器 ,是一个 监测网络中 DNS 服务器是否能正确实现域名解析的命令行工具。 它在 Windows NT/2000/XP 中均可使用 , 但在 Windows 98 中却没有集成这一个工具。
4. explorer-------打开资源管理器
5. logoff---------注销命令
6. shutdown-------60秒倒计时关机命令
7. lusrmgr.msc----本机用户和组
8. services.msc---本地服务设置
9. oobe/msoobe /a----检查XP是否激活
10. notepad--------打开记事本
11. cleanmgr-------垃圾整理
12. net start messenger----开始信使服务
13. compmgmt.msc---计算机管理
14. net stop messenger-----停止信使服务
15. conf-----------启动netmeeting
16. dvdplay--------DVD播放器
17. charmap--------启动字符映射表
18. diskmgmt.msc---磁盘管理实用程序
19. calc-----------启动计算器
20. dfrg.msc-------磁盘碎片整理程序
21. chkdsk.exe-----Chkdsk磁盘检查
22. devmgmt.msc--- 设备管理器
23. regsvr32 /u *.dll----停止dll文件运行
24. drwtsn32------ 系统医生
25. rononce -p----15秒关机
26. dxdiag---------检查DirectX信息
27. regedt32-------注册表编辑器
28. Msconfig.exe---系统配置实用程序
29. rsop.msc-------组策略结果集
30. mem.exe--------显示内存使用情况
31. regedit.exe----注册表
32. winchat--------XP自带局域网聊天
33. progman--------程序管理器
34. winmsd---------系统信息
35. perfmon.msc----计算机性能监测程序
36. winver---------检查Windows版本
37. sfc /scannow-----扫描错误并复原
38. taskmgr-----任务管理器(2000/xp/2003
40. wmimgmt.msc----打开windows管理体系结构(WMI)
41. wupdmgr--------windows更新程序
42. wscript--------windows脚本宿主设置
43. write----------写字板
45. wiaacmgr-------扫描仪和照相机向导
46. winchat--------XP自带局域网聊天
49. mplayer2-------简易widnows media player
50. mspaint--------画图板
51. mstsc----------远程桌面连接
53. magnify--------放大镜实用程序
54. mmc------------打开控制台
55. mobsync--------同步命令
57. iexpress-------木马捆绑工具,系统自带
58. fsmgmt.msc-----共享文件夹管理器
59. utilman--------辅助工具管理器
61. dcomcnfg-------打开系统组件服务
62. ddeshare-------打开DDE共享设置
110. osk------------打开屏幕键盘
111. odbcad32-------ODBC数据源管理器
112. oobe/msoobe /a----检查XP是否激活
68. ntbackup-------系统备份和还原
69. narrator-------屏幕“讲述人”
70. ntmsmgr.msc----移动存储管理器
71. ntmsoprq.msc---移动存储管理员操作请求
72. netstat -an----(TC)命令检查接口
73. syncapp--------创建一个公文包
74. sysedit--------系统配置编辑器
75. sigverif-------文件签名验证程序
76. ciadv.msc------索引服务程序
77. shrpubw--------创建共享文件夹
78. secpol.msc-----本地安全策略
79. syskey---------系统加密,一旦加密就不能解开,保护windows xp系统的双重密码
80. services.msc---本地服务设置
81. Sndvol32-------音量控制程序
82. sfc.exe--------系统文件检查器
83. sfc /scannow---windows文件保护
84. ciadv.msc------索引服务程序
85. tourstart------xp简介(安装完成后出现的漫游xp程序)
86. taskmgr--------任务管理器
87. eventvwr-------事件查看器
88. eudcedit-------造字程序
89. compmgmt.msc---计算机管理
90. packager-------对象包装程序
91. perfmon.msc----计算机性能监测程序
92. charmap--------启动字符映射表
93. cliconfg-------SQL SERVER 客户端网络实用程序
94. Clipbrd--------剪贴板查看器
95. conf-----------启动netmeeting
96. certmgr.msc----证书管理实用程序
97. regsvr32 /u *.dll----停止dll文件运行
98. regsvr32 /u zipfldr.dll------取消ZIP支持

References


















No comments