tag:blogger.com,1999:blog-43820423567473666612024-03-21T06:55:56.610+01:00PowerShell ScriptsPowerShell Scripts RepositoryPablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.comBlogger47125tag:blogger.com,1999:blog-4382042356747366661.post-767619273843103912007-02-23T11:45:00.000+01:002007-02-23T11:47:01.006+01:00Enable or disable Remote Desktop<strong>Description</strong><br /><br />Enable or disable Remote Desktop<br /><br /><strong>Data Source</strong><br /><br />$Terminal = Get-WmiObject Win32_Terminal –Computer "ComputerName"<br />$Terminal.Enable($True)Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-15254255047698005962007-02-15T18:41:00.000+01:002007-02-15T18:43:10.751+01:00Convert or export word documents to PDF<strong>Description</strong><br /><br />Convert or export word documents to PDF<br /><br /><strong>Data Source</strong><br /><br />#You need to install "Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS"<br />$word = new-object -ComObject "word.application"<br />$doc = $word.documents.open("C:\document.doc")<br />$saveaspath = [ref] "c:\document.pdf"<br />$formatPDF = [ref] 17<br />$doc.SaveAs($saveaspath,$formatPDF)<br />$doc.Close()Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com1tag:blogger.com,1999:blog-4382042356747366661.post-1440016338614988982007-02-15T18:39:00.000+01:002007-02-15T18:41:34.562+01:00Convert or export word documents to XPS<strong>Description</strong><br /><br />Convert or export word documents to XPS<br /><br /><strong>Data Source</strong><br /><br />#You need to install "Microsoft Office 2007 Add-in: Microsoft Save as PDF or XPS"<br />$word = new-object -ComObject "word.application"<br />$doc = $word.documents.open("C:\document.doc")<br />$saveaspath = [ref] "c:\document.xps"<br />$formatXPS = [ref] 18<br />$doc.SaveAs($saveaspath,$formatXPS)<br />$doc.Close()Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-77706039576932700472007-02-15T18:36:00.000+01:002007-02-15T18:38:15.885+01:00Send mails from powershell<strong>Description</strong><br /><br />Send mails from powershell<br /><br /><strong>Source Code</strong><br /><br />$smtp = New-Object system.net.mail.smtpclient("smtp.server.com")<br />$smtp.send("<a href="mailto:fromuser@hotmail.com">fromuser@hotmail.com","touser@hotmail.com",</a> "PowerShell script sample","You can see more powershell scripts in <a href="http://powershellscripts.blogspot.com/">http://powershellscripts.blogspot.com</a>")Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-50868075458247198202007-02-14T16:06:00.000+01:002007-02-14T16:07:55.056+01:00Connect to Sql Server and execute SQL statements<strong>Description</strong><br /><br />Connect to Sql Server and execute SQL statements<br /><br /><strong>Data Source</strong><br /><br />$Table = new-object System.Data.DataTable<br />$sqlConn = new-object System.Data.SqlClient.SqlConnection("Data Source=Server\sqlexpress;Initial Catalog=dbProducts;Integrated Security=True")<br />$adapter = new-object System.Data.SqlClient.SqlDataAdapter("Select * from Products",$sqlConn)<br />$adapter.Fill($Table)<br />write-output $tablePablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com1tag:blogger.com,1999:blog-4382042356747366661.post-70778489406261076052007-02-14T13:51:00.000+01:002007-02-14T13:54:15.164+01:00Search Strings In Word Documents<strong>Description</strong><br /><br />Search Strings In Word Documents<br /><br /><strong>Data Source</strong><br /><br />$SearchText = $args[0]<br />$word = new-object -ComObject "word.application"<br />$path = pwd<br />if ($args.length > 1) { <br />$docs = $args[1]<br />}<br />else { <br />$docs = "*.doc"<br />}<br />foreach ($a in $(get-childitem $docs -name)) { <br />$doc = $word.documents.open("$path\$a")<br />if ($doc.content.find.execute("$SearchText")) {<br />write-host $a<br />} <br />$doc.close();<br />}Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-40996821984931612742007-02-14T13:31:00.000+01:002007-02-14T13:36:28.364+01:00Archive and pack IIS log files<strong>Description</strong><br /><br />Archive and pack IIS log files.<br /><br /><strong>Source Code</strong><br /><br />$WinRar = "C:\Program Files\WinRAR\rar"<br />$TodaysLogFile = get-date -Uformat %y%m%d<br />$TodaysLogFile = "ex$TodaysLogFile.log"<br />$LogFiles = ls *.log<br />if ($LogFiles) { <br />foreach ($File in $LogFiles) {<br /> $FileName = $File.Name<br /> if ($FileName -ne $TodaysLogFile) {<br /> echo "Compressing $FileName" <br /> &$WinRar m "$FileName.rar" $FileName <br /> }<br /> }<br />}Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-52072271897233401572007-02-09T10:18:00.000+01:002007-02-09T10:18:21.144+01:00Removing Applications locally or remotelly (PowerShell and WMI)<strong>Description</strong><br /><br />Removing Applications locally or remotelly<br /><br /><strong>DataSource</strong><br /><br />(Get-WmiObject -Class Win32_Product -Filter "Name='ILMerge'" - ComputerName PC01).InvokeMethod("Uninstall",$null)Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-25459258002443582202007-02-09T10:16:00.000+01:002007-02-09T10:14:22.908+01:00Installing Applications Locally or remotely (PowerShell and WMI)<strong>Description</strong><br /><br />Installing Applications Locally or remotely.<br /><br /><strong>DataSource</strong><br /><br />(Get-WMIObject -ComputerName PC01 -List Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).InvokeMethod("Install","\\AppSrv\dsp\NewPackage.msi")Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-42241717080565674792007-02-09T10:12:00.000+01:002007-02-09T10:10:26.319+01:00Displaying Service Status and State for local or remote computers (PowerShell and WMI)<strong>Description</strong><br /><br />Displaying Service Status and State for local or remote computers with powershell.<br /><br /><strong>Source code</strong><br /><br />Get-WmiObject -Class Win32_Service -ComputerName . Select-Object -Property Status,Name,DisplayName,StatePablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-59125871914587572062007-02-09T10:07:00.000+01:002007-02-09T10:06:17.401+01:00Getting Available Disk Space (PowerShell and WMI)<strong>Description</strong><br /><br />Getting Available Disk Space. You need to see only instances with a DriveType of 3—the value WMI uses for fixed hard disks.<br /><br /><strong>Source Code</strong><br /><br />Get-WmiObject -Class Win32_LogicalDisk -Filter "DriveType=3" -ComputerName .Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-19749813527841764632007-02-09T10:05:00.000+01:002007-02-09T10:04:26.138+01:00Listing Installed Hotfixes (using PowerShell and WMI)<strong>Description</strong><br /><br />Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName . (You can replace the trailing dot with a remote computer name).<br /><br /><strong>Source Code</strong><br /><br />Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-71114351353015425962007-02-09T10:02:00.000+01:002007-02-09T10:01:37.703+01:00Listing BIOS Information (using powershell and WMI)<strong>Description</strong><br /><br />Listing BIOS Information (using powershell and WMI). You can replace the trailing dot with a remote computer name.<br /><br /><strong>Source Code</strong><br /><br />Get-WmiObject -Class Win32_BIOS -ComputerName .Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-83201190854871308972007-02-09T09:59:00.000+01:002007-02-09T09:58:03.023+01:00Stopping, Starting, Suspending, and Restarting Services<strong>Description</strong><br /><br />Stopping, Starting, Suspending, and Restarting Services with powershell (spooler service in this example)<br /><br /><strong>Source Code</strong><br /><br />Stop-Service -Name spooler<br /><br />or<br /><br />Start-Service -Name spooler<br /><br />or<br /><br />Suspend-Service -Name spooler<br /><br />or<br /><br />Restart-Service -Name spoolerPablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-65415407865635163832007-02-09T09:56:00.000+01:002007-02-08T19:37:43.240+01:00Stop all nonresponsive applications<strong>Description</strong><br /><br />You can stop all nonresponsive applications with powershell<br /><br /><strong>Source Code</strong><br /><br />Get-Process Where-Object -FilterScript {$_.Responding -eq $false} Stop-ProcessPablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-22983071290888537662007-02-08T19:21:00.000+01:002007-02-08T19:25:16.244+01:00Create users in a OU<strong>Description</strong><br /><br />Here we can see how to create users in a Organizational Unit (OU).<br /><br /><strong>Source Code</strong><br /><br />$OU= New-Object DirectoryServices.DirectoryEntry "<a href="ldap://OU=SampleMustExists,DC=powershellscripts,DC=blogspot,DC=com">LDAP://OU=SampleMustExists,DC=powershellscripts,DC=blogspot,DC=com</a>"<br />$User=$OU.Create("user","CN=John")<br />$User.put("profilepath","<a href="file:////server/share">\\server\share</a>")<br />$User.SetInfo()Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-30558681367382558342007-02-08T19:17:00.000+01:002007-02-08T17:43:14.685+01:00Create an OU in a domain<strong>Description</strong><br /><br />Here we can see how to create an OU in a domain.<br /><br /><strong>Source Code</strong><br /><br />$Domain= New-Object DirectoryServices.DirectoryEntry "<a href="ldap://DC=powershellscripts,DC=blogspot,DC=com">LDAP://DC=powershellscripts,DC=blogspot,DC=com</a>"<br />$OU = $Domain.Create("organizationalUnit", "ou=PowerShell")<br />$OU.SetInfo()Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-12854888567968697112007-02-08T17:41:00.000+01:002007-02-08T17:36:48.569+01:00Map network drives with powershell<strong>Description</strong><br /><br />Map network drives with powershell<br /><br /><strong>Source Code</strong><br /><br />$net = $(New-Object -ComObject WScript.Network)<br />$net.MapNetworkDrive("x:", <a href="file:////servername/sharedfolder">\\servername\sharedfolder</a>)Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-34605676252629692422007-02-08T17:28:00.000+01:002007-02-06T10:59:30.777+01:00List Users from a Organizational Unit (OU)<strong>Description</strong><br /><br />List Users from a Organizational Unit (OU).<br /><br /><strong>Source Code</strong><br /><br />$Dom = "<a href="ldap://OU=OfficeUsers,DC=powershellscripts,DC=blogspot,DC=com/">LDAP://OU=OfficeUsers,DC=powershellscripts,DC=blogspot,DC=com</a>"<br />$Root = New-Object DirectoryServices.DirectoryEntry $Dom<br /><br /># Create a selector and start searching from the Root of AD<br />$selector = New-Object DirectoryServices.DirectorySearcher<br />$selector.SearchRoot = $root<br /># Filter the users with -like "CN=Person*". Note the ForEach loop<br />$adobj= $selector.findall() where {<br />$_.properties.objectcategory -like "CN=Person*"<br />}<br />foreach ($person in $adobj)<br />{<br />$prop=$person.properties<br />Write-host "First name: $($prop.givenname) Surname: $($prop.sn) User: $($prop.cn)"<br />}<br />Write-host "There are $($adobj.count) users in the $($root.name) domain"Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com3tag:blogger.com,1999:blog-4382042356747366661.post-35082729572737413482007-02-05T13:57:00.000+01:002007-02-05T14:02:06.257+01:00Adding New Windows PowerShell Drives (New-PSDrive)<strong>Description</strong><br /><br />To create a new Windows PowerShell drive, you must supply three parameters:<br />· A name for the drive (you can use any valid Windows PowerShell name)<br />· The PSProvider (use "FileSystem" for file system locations and "Registry" for registry locations)<br />· The root, that is, the path to the root of the new drive<br /><br /><strong>Source Code</strong><br /><br />New-PSDrive -Name Startup -PSProvider Registry -Root HKLM\Software\Microsoft\Windows\CurrentVersion\Run<br /><br />Now we can use <em>cd Startup:</em>Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-34432490953773949992007-02-05T13:53:00.000+01:002007-02-05T13:55:27.344+01:00Printing Data from Powershell<strong>Description</strong><br /><br />You can print data by using the Out-Printer cmdlet. The Out-Printer cmdlet will use your default printer if you do not provide a printer name.<br /><br /><strong>Source Code</strong><br /><br />Get-Command Out-Printer -Name "Microsoft Office Document Image Writer"Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com1tag:blogger.com,1999:blog-4382042356747366661.post-16055995635888168122007-02-05T13:30:00.000+01:002007-02-05T13:38:37.201+01:00Messagebox in powershell<strong>Description</strong><br /><br />Show Messagebox in powershell. First we need to add a reference to an assembly to out script.<br /><br /><strong>Source Code</strong><br /><br />[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")<br />[Windows.Forms.MessageBox]::Show("Hello World!", "PowerShellScripts.blogspot.com", [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question)Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-74830688088266612182007-02-05T11:38:00.000+01:002007-02-05T12:51:27.940+01:00Navigating the Registry<strong>Description</strong><br /><br />You can navigate through the Windows registry by using the same techniques that you use to navigate in the file system drive. In Windows PowerShell, the HKEY_LOCAL_MACHINE hive maps to the Windows PowerShell HKLM: drive and the HKEY_CURRENT_USER drive maps to the Windows PowerShell HKCU: drive.<br /><br /><strong>Source Code</strong><br /><br />cd HKLM:<br /><br /><strong>or</strong><br /><br />CD HKCU:Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com2tag:blogger.com,1999:blog-4382042356747366661.post-58571589973300164862007-02-05T10:28:00.000+01:002007-02-05T10:29:25.295+01:00Export Mailbox Information to a Text File<p><strong>Description</strong></p><p>Exports information about all mailboxes to a commas-separated values file named Test.csv. This script/command requires Microsoft Exchange Server 2007. </p><p><strong>Script Code</strong></p><p>get-mailbox export-csv c:\scripts\test.csv</p>Pablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0tag:blogger.com,1999:blog-4382042356747366661.post-6044847321440707452007-02-05T10:26:00.000+01:002007-02-05T10:27:22.339+01:00Set Message Size Restrictions<strong>Description</strong><br /><br />Sets message size restrictions for the distribution group FinanceUsers. This script/command requires Microsoft Exchange Server 2007.<br /><br /><strong>Script Code</strong><br /><br />set-DistributionGroup FinanceUsers -MaxReceiveSize 50KBPablohttp://www.blogger.com/profile/09125665488520243369noreply@blogger.com0