Python – Get Common Operating System Values

I was converting one of my AD solutions from PowerShell to Python (hobby that I have developed recently to pass time on holidays )

That solution required getting current DC & current directory which I have found is available in os library.

So, I thought lets blog about all the common things that you can do with this library, as these are common values required in day-to-day scripting.

How to fetch the domain name, domain controller, hostname, current directory, current login etc.?

Get HostName:

Import os


Get Logon Domain Controller:

Import os


Get Domain:

Import os


Get Current User:

Import os




You will be able to get pretty much everything you can get with set command on cmd.


Get Current Working Directory:

Import os


Change Directory:

Import os


Make Directory:

Import os


List Directories:

Import os


To find all the commands in OS module just type dir(os):

os library is very handy for a system administrator working with python.

I hope examples I have quoted here are helpful for the community.


Thanks for reading …

Tech Wizard

PowerShell Fast Track


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s