10 PowerShell One Liners

Here are a few one-liners that use NetCmdlets. Some of these I've blogged about before, some are new. Let me know if you have questions, which ones you find useful, or how you altered these to suit your own needs.

  1. Send email to a list of recipient addresses:
    import-csv users.csv | % { send-email -to $_.email -from lance@nsoftware.com -subject "Important Email" –message "Hello World!" -server }
  2. Show the access control list for a specific Exchange folder:
    get-imap -server $mymailserver -cred $mycred -folder INBOX.RESUMES –acl
  3. Add look and read permissions on an Exchange folder, for a list of accounts pulled from a CSV file:
    import-csv users.csv | % { set-imap -server -acluser $_.username $mymailserver -cred $mycred -folder INBOX.RESUMES –acl “lr”  }
  4. Sync system time with an Internet time server:
    get-time -server clock.psu.edu –set
    To remotely sync the time on a set of computers:
    import-csv computers.csv | % { Invoke-Command -computerName $_.computer -cred $mycred -scriptblock { get-time -server clock.psu.edu –set } }
  5. Delete all emails from an Exchange folder that match a certain criteria.  For example, delete all emails from alf@email.com:
    get-imap -server $mailserver –cred $mycred | ? {$_.FromEmail -eq alff@email.com} | %{ set-imap -server $mailserver –cred $mycred-message $_.Id -delete }
  6. Update Twitter status from PowerShell:
    get-http –url "http://twitter.com/statuses/update.xml" –cred $mycred -variablename status -variablevalue "Tweeting with NetCmdlets!"
  7. A test-path that works over FTP, FTPS (SSL), and SFTP (SSH) connections:
    get-ftp -server $remoteserver –cred $mycred -path /remote/path/to/checkfor*
    Don't forget the *.  Also, to use SSL or SSH just add an –ssl or –ssh parameter.
  8. List disabled user accounts in Active Directory (or any other LDAP server):
    get-ldap -server $ad -cred $mycred -dn dc=yourdc -searchscope wholesubtree
        -search "(&(objectclass=user)(objectclass=person)(company=*)(userAccountControl:1.2.840.113556.1.4.803:=2))"
  9. List Active Directory groups and their members:
    get-ldap -server testman -cred $mycred -dn dc=NS2 -searchscope wholesubtree -search "(&(objectclass=group)(cn=*admin*))" | select ResultDN, member
  10. Display the last initialization time (e.g. last reboot time) of all discoverable SNMP agents on a network:
    import-csv computers.csv | % { get-snmp -agent $_.computer -oid sysUpTime.0 | %{([datetime]::Now).AddSeconds(-($_.OIDValue/100))} }

Not mentioned here:  data conversion (Yenc, QP, UUencoding, MD5, SHA1, base64, etc), DNS, News Groups (NNTP/UseNet), POP mail, RSS feeds, Amazon S3, Syslog, TFTP, TraceRoute, SNMP Traps, UDP, WebDAV, whois, Rexec/Rshell/Telnet, Zip files, sending IMs (Jabber/GoogleTalk/XMPP), sending text messages and pages, ping, and more.

Original Source: Lance's Textbox

Print | posted on Wednesday, April 21, 2010 5:20 PM


No comments posted yet.

Your comment:


Copyright © Lance Robinson

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski