Given a finite-dimensional vector space V over the finite field F q and a subspace W of V , we consider the problem of counting linear transformations T : W → V which have prescribed invariant factors. The case W = V is a well-studied problem that is essentially equivalent to counting the number of square matrices over F q in a conjugacy class and an explicit formula is known in this case. On the other hand, the case of general W is also an interesting problem and there hasn't been substantive progress in this case for over two decades, barring a special case where all the invariant factors of T are of degree zero. We extend this result to the case of arbitrary W by giving an explicit counting formula. As an application of our results, we give new proofs of some recent enumerative results in linear control theory and derive an extension of the Gerstenhaber-Reiner formula for the number of square matrices over F q with given characteristic polynomial.